2010-03-21 111 views
2

在我使用jQuery的頁面上運行驗證會給我很多錯誤。 我逃過了結束標記,但不斷收到錯誤。使用jQuery W3C標記驗證錯誤

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

回答

4

假設你使用XHTML作爲DOCTYPE你應該換行的js代碼包含HTML片段,片段CDATA

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

爲什麼?:Mozilla Dev: Properly Using CSS and JavaScript in XHTML Documents

+0

喝彩!感謝「爲什麼?」 – FFish 2010-03-21 10:58:34

1

這樣做:

<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。基本的是,Javascript標籤通常是CDATA元素,PCDATA帶有XHTML(所以它看起來內部),爲了安全起見,他們需要用這種方式標記。

+0

感謝您的鏈接尼克 – FFish 2010-03-21 10:57:50