在HTML属性中添加引号

时间:2013-12-12 07:39:11

标签: javascript html escaping

例如,

<span class="hotspot" 
    onmouseover="tooltip.show('<table id=\"test\"><tr><th>123</th></tr><tr><th>123</th></tr></table>');" 
    onmouseout="tooltip.hide();">porttitor orci</span> 

在这里,我试图在表格标签中添加id属性,但我已经使用了&#34;&#34;和&#39;&#39;,所以有些东西搞砸了,不能正常工作。

有什么好的解决方案吗?

2 个答案:

答案 0 :(得分:4)

<span class="hotspot" 
    onmouseover="tooltip.show('<table id=&quot;test&quot;><tr><th>123</th></tr><tr><th>123</th></tr></table>');" 
    onmouseout="tooltip.hide();">porttitor orci</span> 

JSFiddle

答案 1 :(得分:1)

在由Ascii引号(“)分隔的属性值中,您可以使用引用&quot;呈现Ascii引号。

然而,通常有更好的方法。在给定的情况下,您可以简单地省略引号,因为id=test工作正常(除非您使用的是XML内容类型的XHTML,并且很可能不是)。