CDATA真的有必要吗? [英] Is CDATA really necessary?

查看:95
本文介绍了CDATA真的有必要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用内联Javascript,通常在我制作的WordPress主题中。直到几个月前,我还没有听说过在 //<![CDATA [... //]]> 中包装内联Javascript,我一直在以相当的能力完成这些工作几年。

I use inline Javascript quite a bit, usually in WordPress themes that I make. I had not heard of wrapping inline Javascript in //<![CDATA[ ... //]]> up until a few months ago, and I have been doing this stuff at a fair level of competency for a few years.

我google了一下,我听说人们使用它,因为他们的Javascript不会验证。我使用严格的1.0 xHTML文档类型,并且在验证我的标记时从未遇到过问题。是因为我使用jquery,还是因为我通常只有几行代码来激活插件?或者w3验证器在这方面是否宽松?不使用这些CDATA标记时是否有任何功能影响的证据?

I googled around and I hear that people use this because their Javascript doesn't validate otherwise. I use a strict 1.0 xHTML doctype and have never had a problem validating my markup. Is it because I use jquery, or because usually I only have a few lines of code to activate a plugin? Or is the w3 validator being lenient in this regard? Is there any evidence of functional impact when not using these CDATA markings?

推荐答案

今天只有你需要你的XHTML文件才真正需要它有效。例如这个简单的东西是无效的,因为javascript中的< p> 标签与CDATA验证

Today it's only really required if you want your XHTML document to be valid. e.g. something as simple as this is invalid because of the <p> tags in the javascript with the CDATA it validates

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  <title>foo</title>
  <script type="text/javascript">
    alert("<p>Hallo</p>");
  </script>
</head>
<body>
</body>
</html>

这篇关于CDATA真的有必要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆