在JavaScript / HTML中嵌套引号 [英] Nesting quotes in JavaScript/HTML
问题描述
你如何在HTML中嵌套超出第二级的引号?据我所知,只有两种类型的引号 - single(')和double()。我知道使用斜杠转义 - 你必须在代码中转义,但转义不会在浏览器级别工作。什么是可以接受的方法来解决下面的问题?
< p onclick =exampleFunc('< div id =divId>< / div>');>一些文字< / p>
该代码打印到浏览器:
');>一些文本
您需要使用正确的转义/编码。无论是在使用字符引用的HTML中:
< p onclick =exampleFunc('& lt; div id =& quot ; divId&& gt;& lt; / div& gt;');>一些文字< / p>
或者在JavaScript中使用字符串转义序列:
< p onclick =exampleFunc('\x3Cdiv\x20id\x3D\x22divId\x22\x3E\x3C / div\x3E'); >一些文字< / p>
How do you nest quotes in HTML beyond the second level? As far as I know, there are only 2 types of quotes - single(') and double("). I am aware of escaping using slashes - you have to escape in the code but that escaping won't work at the browser level. What is the accepted method to get around something like the following?
<p onclick="exampleFunc('<div id="divId"></div>');">Some Text</p>
That code prints to the browser:
');">Some Text
You need to use proper escaping/encoding. Either in HTML using character references:
<p onclick="exampleFunc('<div id="divId"></div>');">Some Text</p>
Or in JavaScript using string escape sequences:
<p onclick="exampleFunc('\x3Cdiv\x20id\x3D\x22divId\x22\x3E\x3C/div\x3E');">Some Text</p>
这篇关于在JavaScript / HTML中嵌套引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!