反斜线转义和html转义区别?

  var txt="We are the so-called \"Vikings\" from the north."
  var txt2="We are the so-called "Vikings" from the north."
  document.write(txt2)

上面两种效果一样,那这两种有什么区别?

阅读 4.9k
2 个回答

把第一个字符串插入到HTML中时,插入的实际字符串是

We are the so-called "Vikings" from the north.

反斜杠被JavaScript转义

第二个插入的是

We are the so-called "Vikings" from the north.

在显示的时候浏览器会转义第二个字符串中的"

第一种后端可能会当作转义字符再次处理,第二种好

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题