Home  >  Article  >  Web Front-end  >  Description of the role of CDATA in the page_Experience exchange

Description of the role of CDATA in the page_Experience exchange

WBOY
WBOYOriginal
2016-05-16 12:08:091474browse

Must be <script> 和 <style> 元素的内容包装到 CDATA 节中。  <br><br>如果在脚本或样式表中使用特殊字符(例如 < 或 &)或实体引用(例如 < 或 &),则需要将脚本或样式表的内容标记为 CDATA(字符数据)节,如下所示。  <br><br><script type="text/javascript"> <BR><![CDATA[ <br><br>function isLess(a, b) { <BR>  if (a < b) <BR>    return true; <BR>} <br><br>]]> <BR></script>

Using a CDATA section does not work in all browsers. For example, Internet Explorer will <script> 标记中的 CDATA 节视为语法错误。可以通过添加 JavaScript 注释避免该问题,如下所示。  <br><br><script type="text/javascript"> <BR>/* <![CDATA[ */ <br><br>function isLess(a, b) { <BR>  if (a < b) <BR>    return true; <BR>} <br><br>/* ]]> */ <BR></script>

JavaScript uses /* and */ to mark the beginning and end of comments. Therefore, the CDATA section is hidden from JavaScript, but not from browsers parsing the page. In summary, it is better practice to place style rules and scripts in external files and reference these files from the XHTML page. By using external style sheets and scripts, you can avoid all of the above problems.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn