Nesting quotes in JavaScript/HTML

做~自己de王妃 提交于 2019-11-27 04:29:21

You need to use proper escaping/encoding. Either in HTML using character references:

<p onclick="exampleFunc('&lt;div id=&quot;divId&quot;&gt;&lt;/div&gt;');">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>

Edit: this is not a solution for JavaScript in HTML, but for JavaScript only. My bad...

eval('eval(\"eval(\\\"alert(\\\\\\\"Now I\\\\\\\\\\\\\\\'m confused!\\\\\\\")\\\")\")');

Link. It's "recursive escaping".

you need to escape the characters using the \

so your code should look like

<p onclick="exampleFunc('<div id=\"divId\"></div>');">Some Text</p>

Here is some info on Special Characters

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