究竟什么是CDATA,它做了什么?

时间:2022-05-21 12:22:52

I sometimes notice a CSS or JS code surrounded with <![CDATA[ and ]]> tags. I couldn't find any explanation to this. What does CDATA do? Why is it used by some people and what is it needed for?

我有时会注意到用 标签包围的CSS或JS代码。我找不到任何解释。 CDATA做什么?为什么有些人会使用它以及它需要什么?

4 个解决方案

#1


16  

It tells the interpreter that it should not try to interpret the data enclosed in the tags. For example, if you want a XML file to contain a comment with < or >, XML interpreters will report the file as invalid because the caracters < and > will not be part of standard tag. You simply have to surround the code with the CDATA tags.

它告诉解释器它不应该尝试解释标签中包含的数据。例如,如果您希望XML文件包含带有 <或> 的注释,则XML解释器会将该文件报告为无效,因为caracters <和> 不会成为标准标记的一部分。您只需使用CDATA标记包围代码即可。

#2


8  

It marks data that shouldn't be parsed - in most cases, just HTML or text that needs to be written directly to the page.

它标记了不应解析的数据 - 在大多数情况下,只标记需要直接写入页面的HTML或文本。

Further explanation: http://www.w3schools.com/xml/xml_cdata.asp

进一步说明:http://www.w3schools.com/xml/xml_cdata.asp

#3


6  

Here's some info for you: http://en.wikipedia.org/wiki/CDATA

以下是您的一些信息:http://en.wikipedia.org/wiki/CDATA

How you couldn't find anything on this is a mystery ;)

你怎么找不到这个是一个谜;)

#4


1  

When you write inline javascript or CSS in an HTML file (using the script and style tags), Those CDATA comments prevent things like the HTML validators from catching false positives when validating an HTML document.

当您在HTML文件中编写内联javascript或CSS(使用脚本和样式标记)时,那些CDATA注释会阻止HTML验证程序在验证HTML文档时捕获误报。

#1


16  

It tells the interpreter that it should not try to interpret the data enclosed in the tags. For example, if you want a XML file to contain a comment with < or >, XML interpreters will report the file as invalid because the caracters < and > will not be part of standard tag. You simply have to surround the code with the CDATA tags.

它告诉解释器它不应该尝试解释标签中包含的数据。例如,如果您希望XML文件包含带有 <或> 的注释,则XML解释器会将该文件报告为无效,因为caracters <和> 不会成为标准标记的一部分。您只需使用CDATA标记包围代码即可。

#2


8  

It marks data that shouldn't be parsed - in most cases, just HTML or text that needs to be written directly to the page.

它标记了不应解析的数据 - 在大多数情况下,只标记需要直接写入页面的HTML或文本。

Further explanation: http://www.w3schools.com/xml/xml_cdata.asp

进一步说明:http://www.w3schools.com/xml/xml_cdata.asp

#3


6  

Here's some info for you: http://en.wikipedia.org/wiki/CDATA

以下是您的一些信息:http://en.wikipedia.org/wiki/CDATA

How you couldn't find anything on this is a mystery ;)

你怎么找不到这个是一个谜;)

#4


1  

When you write inline javascript or CSS in an HTML file (using the script and style tags), Those CDATA comments prevent things like the HTML validators from catching false positives when validating an HTML document.

当您在HTML文件中编写内联javascript或CSS(使用脚本和样式标记)时,那些CDATA注释会阻止HTML验证程序在验证HTML文档时捕获误报。