Escaping HTML in jQuery so it's valid

别来无恙 提交于 2019-12-24 07:30:33

问题


I've followed advice from firebug and W3C links, however when Firebug confirms all is valid, W3C does not seem to like it and vice versa.

All I'm doing is this:

return '<a href="#"></a>';

I believe I should use a backslash infront of the forward slash, though as XHTML, the start tag needs to be escaped as well? Can anyone shed any light on this.

Thanks.


回答1:


Short answer: Don't use XHTML on the client.

Almost the short answer: Put your JavaScript in external files

Longer answer: If you really want to embed the JS, then wrap it with CDATA flags

Really long answer: Read http://dorward.me.uk/www/comments-cdata/




回答2:


If you wrap JS in CDATA tags, you don't need to escape the HTML.

<script type="text/javascript">
/* <![CDATA[ */
function(){
    return '<a href="#"></a>';
};
/* ]]> */
</script>


来源:https://stackoverflow.com/questions/5298881/escaping-html-in-jquery-so-its-valid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!