문제

Running a validation on my pages where I used jQuery gives me lots of errors.. I escaped the closing tags but keep getting errors.

<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

<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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top