1

文字列パラメーターを使用した 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&#39;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>

マウスオーバーするとエラーが発生します

4

2 に答える 2

2

問題は、JavaScript が評価される前に&#39;が に変換されていることです。'したがって、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にどのように終了しているかに注意してください。残りはコードとして処理され、一致しない閉じ引用符が追加されています'。文字列が JavaScript で適切に引用されるようにするには、'inの前にバックスラッシュを付ける必要があります。manufacturer's

a class="as_Glossary" onmouseover="unescapeHTML('The manufacturer\&#39;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
}
于 2010-01-08T19:23:35.727 に答える
0

その文字参照の後にセミコロンが必要です

于 2010-01-08T18:54:27.830 に答える