2010-01-08 43 views
1

我有一個字符串參數的javascript方法調用。在字符串文本中有時包含html字符引用,例如'我收到一個意外的標識符錯誤。如果我有字符引用"那麼它工作正常。不知道爲什麼。以下是我正在嘗試執行的代碼片段。實際的方法要長得多,並試圖做一些不同於我在這裏展示的東西,但是這個片段應該能夠重現錯誤。Javascript字符串參數與字符引用

<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> 

當我將鼠標放置到我的錯誤

+0

之間不存在charachter 9和分號對吧? – 2010-01-08 18:52:33

+1

你可能會發布JavaScript源代碼或至少是它的餅乾嗎?這增加了相當高質量的答案的時間;) – Juri 2010-01-08 18:56:56

+0

謝謝Juri,我添加了一些片段來演示問題 – Eqbal 2010-01-08 19:18:12

回答

2

的問題是,你的&#39;被轉換爲' 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\&#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 
} 
+0

很酷。得到它了!似乎現在工作。感謝Brian。 – Eqbal 2010-01-08 19:31:59

0

你需要一個分號該字符參考