问题在同一行上呈现单引号和双引号

时间:2017-06-08 23:53:01

标签: jquery html-escape-characters

我有一个循环,其中我试图添加这行代码

实施例

<div class="wb-share" data-wb-share='{"pnlId": "pnl1"}'></div>

我的实际代码是

var share = "<div class=\"wb-share\" data-wb-share='{\"pnlId\": \"pnl" + Id + "\"}'></div>";

呈现为

<div class="wb-share" data-wb-share="{&quot;pnlId&quot;: &quot;pnl206259534&quot;}"></div>

1 个答案:

答案 0 :(得分:0)

使用innerHTML方法并将这些字符转义,请检查此代码段的浏览器检查器。

&#13;
&#13;
var share = "<div class=\"wb-share\" data-wb-share='{\"pnlId\": \"pnl1\"}'></div>";
document.getElementById('app').innerHTML = share
&#13;
.wb-share{ background: #000; width: 100px; height: 100px;}
&#13;
<div id='app'></div>
&#13;
&#13;
&#13;