字符串中的JavaScript和正斜杠 [英] JavaScript and forward slashes in strings
问题描述
我们必须在JavaScript字符串中转义正斜杠的真正原因是什么,以及为什么我们必须在XHTML中转义字符串/无字符串。很多教程只是简单介绍了这个问题。
What is the real reason that we must escape a forward slash in a JavaScript string, and also why must we escape string/no string in XHTML. A lot of tutorials just brush over this issue.
推荐答案
我们必须在什么地方找到真正的理由。在JavaScript字符串中转义正斜杠
What is the real reason that we must escape a forward slash in a JavaScript string
在HTML 4文档中,序列< /
定义为包含CDATA的元素(如脚本)是一个结束标记并将结束该元素(如果它不是< / script> $ c $则会出错c>。
In an HTML 4 document, the sequence </
inside an element defined as containing CDATA (such as script) is an end tag and will end the element (with an error if it is not </script>
.
就JS而言 /
和 \ /
在字符串中是相同的。就HTML而言< /
启动结束标记但是< \ /
没有。
As far as JS is concerned /
and \/
are identical inside a string. As far as HTML is concerned </
starts an end tag but <\/
does not.
,以及为什么我们必须在XHTML中转义字符串/无字符串。
, and also why must we escape string/no string in XHTML.
XHTML没有提供指定元素本质上包含CDATA的方法,因此您需要显式处理否则具有特殊含义的字符(<
,&
等)。包含它们包含带有CDATA标记的元素是实现此目的的最简单方法。
XHTML doesn't provide a method of specifying that an element intrinsically contains CDATA, so you need to explicitly handle characters which would otherwise have special meaning (<
, &
, etc). Wrapping them contains of the element with CDATA markers is the easiest way to achieve this.
这篇关于字符串中的JavaScript和正斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!