Javascript 什么是 HTML 中的 CDATA?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7092236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is CDATA in HTML?
提问by SexyMF
What is the use of CDATA inside JavaScript tags and HTML?
在 JavaScript 标签和 HTML 中使用 CDATA 有什么用?
<script type="text/javascript">
// <![CDATA[
// ]]>
</script>
采纳答案by Ahsan Rathod
All text in an XML document will be parsed by the parser.
XML 文档中的所有文本都将由解析器解析。
But text inside a CDATA section will be ignored by the parser.
但是 CDATA 部分中的文本将被解析器忽略。
CDATA - (Unparsed) Character Data
CDATA -(未解析的)字符数据
The term CDATA is used about text data that should not be parsed by the XML parser.
Characters like "<" and "&" are illegal in XML elements.
"<" will generate an error because the parser interprets it as the start of a new element.
"&" will generate an error because the parser interprets it as the start of an character entity.
Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.
Everything inside a CDATA section is ignored by the parser.
A CDATA section starts with "
<![CDATA[
" and ends with "]]>
"
术语 CDATA 用于表示不应由 XML 解析器解析的文本数据。
像“<”和“&”这样的字符在 XML 元素中是非法的。
"<" 会产生一个错误,因为解析器将它解释为一个新元素的开始。
"&" 会产生错误,因为解析器将它解释为字符实体的开始。
某些文本(如 JavaScript 代码)包含大量“<”或“&”字符。为了避免错误,脚本代码可以定义为 CDATA。
CDATA 部分中的所有内容都被解析器忽略。
一个CDATA部件开始用“
<![CDATA[
”,并结束与“]]>
”
Use of CDATA in program output
在程序输出中使用 CDATA
CDATA sections in XHTML documents are liable to be parsed differently by web browsers if they render the document as HTML, since HTML parsers do not recognise the CDATA start and end markers, nor do they recognise HTML entity references such as
<
within<script>
tags. This can cause rendering problems in web browsers and can lead to cross-site scripting vulnerabilities if used to display data from untrusted sources, since the two kinds of parsers will disagree on where the CDATA section ends.
如果 Web 浏览器将文档呈现为 HTML,则 XHTML 文档中的 CDATA 部分可能会被 Web 浏览器以不同方式解析,因为 HTML 解析器不识别 CDATA 开始和结束标记,也不识别 HTML 实体引用,例如标签
<
内<script>
。如果用于显示来自不受信任来源的数据,这可能会导致 Web 浏览器中的渲染问题,并可能导致跨站点脚本漏洞,因为这两种解析器在 CDATA 部分的结束位置上存在分歧。
Also, see the Wikipedia entry on CDATA.
回答by Delan Azabani
CDATA
has no meaning at all in HTML.
CDATA
在 HTML 中根本没有任何意义。
CDATA
is an XML construct which sets a tag's contents that is normally #PCDATA - parsed character data, to be instead taken as #CDATA, that is, non-parsed character data. It is only relevant and valid in XHTML.
CDATA
是一种 XML 构造,它设置通常是 #PCDATA - 解析的字符数据的标记内容,改为被视为 #CDATA,即未解析的字符数据。它仅在 XHTML 中相关且有效。
It is used in script
tags to avoid parsing <
and &
. In HTML, this is not needed, because in HTML, script
is already #CDATA.
它用于script
标签以避免解析<
和&
。在 HTML 中,这是不需要的,因为在 HTML 中,script
它已经是 #CDATA。
回答by user823959
From http://en.wikipedia.org/wiki/CDATA:
来自http://en.wikipedia.org/wiki/CDATA:
Since it is useful to be able to use less-than signs (<) and ampersands (&) in web page scripts, and to a lesser extent styles, without having to remember to escape them, it is common to use CDATA markers around the text of inline and elements in XHTML documents. But so that the document can also be parsed by HTML parsers, which do not recognise the CDATA markers, the CDATA markers are usually commented-out, as in this JavaScript example:
由于能够在网页脚本中使用小于号 (<) 和与号 (&) 以及在较小程度上使用样式很有用,而不必记住对它们进行转义,因此通常在周围使用 CDATA 标记XHTML 文档中内联和元素的文本。但是为了使文档也可以被 HTML 解析器解析,它不识别 CDATA 标记,CDATA 标记通常被注释掉,如下面的 JavaScript 示例所示:
<script type="text/javascript">
//<![CDATA[
document.write("<");
//]]>
</script>
回答by Daniel De León
CDATA is Obsolete.
CDATA 已过时。
Note that CDATA sections should not be used within HTML; they only work in XML.
请注意,不应在 HTML 中使用 CDATA 部分;它们只适用于 XML。
So do not use it in HTML 5.
所以不要在 HTML 5 中使用它。
https://developer.mozilla.org/en-US/docs/Web/API/CDATASection#Specifications
https://developer.mozilla.org/en-US/docs/Web/API/CDATASection#Specifications
回答by Tushar Ahirrao
CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows: Replace character entities with characters,
CDATA 是来自文档字符集的字符序列,可能包括字符实体。用户代理应按如下方式解释属性值:用字符替换字符实体,
Ignore line feeds,
忽略换行符,
Replace each carriage return or tab with a single space.
用一个空格替换每个回车符或制表符。