我有一个带有字符串参数的javascript方法调用。在字符串文本中有时包含html字符引用,例如'
我收到了意外的标识符错误。如果我将字符引用设为"
,那么它可以正常工作。不知道为什么会这样。下面是我要做的事情的代码片段。实际方法要长得多,并尝试做一些不同于我在这里展示的内容,但这段代码应该能够重现错误。
<script>
function unescapeHTML(html) {
var htmlNode = document.createElement("div");
htmlNode.innerHTML = html;
if(htmlNode.innerText)
alert htmlNode.innerText; // IE
else
alert htmlNode.textContent; // FF
}
</script>
<a class="as_Glossary" onmouseover="unescapeHTML('The manufacturer's sales in dollars to all purchasers in the United States excluding certain exemptions for a specific drug in a single calendar quarter divided by the total number of units of the drug sold by the manufacturer in that quarter'); return true;" onmouseout="hideGlossary(); return true;">Test</a>
当我鼠标悬停时,我收到错误
答案 0 :(得分:2)
问题是,在评估JavaScript之前,您的'
正在转换为'
。因此,JavaScript会看到以下内容(为了便于阅读而包含):
unescapeHTML('The manufacturer's sales in dollars to all purchasers in
the United States excluding certain exemptions for a specific drug in a
single calendar quarter divided by the total number of units of the drug
sold by the manufacturer in that quarter');
return true;
注意字符串在manufacturer
之后如何结束,其余字符串作为代码处理,带有额外的不匹配的引用引用'
。您需要在'
中使用反斜杠作为manufacturer's
的前缀,以便在JavaScript中正确引用该字符串:
a class="as_Glossary" onmouseover="unescapeHTML('The manufacturer\'s sales...
您的alert
表达式中还需要括号:
function unescapeHTML(html) {
var htmlNode = document.createElement("div");
htmlNode.innerHTML = html;
if(htmlNode.innerText)
alert(htmlNode.innerText); // IE
else
alert(htmlNode.textContent); // FF
}
答案 1 :(得分:0)
在该字符引用后需要分号