简体   繁体   English

jQuery的W3C标记验证错误

[英]W3C Markup Validation errors with jQuery

Running a validation on my pages where I used jQuery gives me lots of errors.. I escaped the closing tags but keep getting errors. 在使用jQuery的页面上运行验证会给我带来很多错误。.我逃脱了结束标记,但不断出错。

<script type="text/javascript">
    $(document).ready(function() {
        $("#main").html('<p>hello world<\/p>'); 
    });
</script>

Assuming you use XHTML as DOCTYPE you should wrap js-code which contains HTML fragments with CDATA 假设您将XHTML用作DOCTYPE,则应该包装JS代码,其中包含带有CDATA的HTML片段

<script type="text/javascript">
    $(document).ready(function() {
        /*<![CDATA[*/
        $("#main").html('<p>hello world</p>');
        /*]]>*/
    });
</script>

Why?: Mozilla Dev: Properly Using CSS and JavaScript in XHTML Documents 为什么?: Mozilla开发人员:在XHTML文档中正确使用CSS和JavaScript

Do this: 做这个:

<script type="text/javascript">
//<![CDATA[
    $(document).ready(function() {
        $("#main").html('<p>hello world</p>'); 
    });
//]]>
</script>

You can read a bit more on the topic here . 您可以在此处阅读有关该主题的更多内容 The basics are that Javascript tags are CDATA elements normally, PCDATA with XHTML (so it looks inside), to be safe they need to be marked up in this way. 基本原理是Javascript标记通常是CDATA元素,带有XHTML的PCDATA(因此它看起来在内部),为了安全起见,必须以这种方式对其进行标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM