在JavaScript / HTML中嵌套引号 [英] Nesting quotes in JavaScript/HTML

查看:105
本文介绍了在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('&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>

这篇关于在JavaScript / HTML中嵌套引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆