2010-01-08 3 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

없습니다를 9와 세미콜론 권리? –

+1

JavaScript 소스 또는 적어도 piecs를 게시 할 수 있습니까? 이것은 고품질의 답변을 상당히 많이 얻을 수있는 시간을 증가시킵니다.) – Juri

+0

감사합니다. Juri, 문제를 보여주기 위해 일부 스 니펫을 추가했습니다. – Eqbal

답변

2

문제는 당신이 자바 스크립트 전에 '로 변환되고있다 &#39;이다 그 문자 참조 후 세미콜론 필요 평가. 문자열이 manufacturer 후에 종료하고, 나머지는 여분의 타의 추종을 불허 가까운 인용 '으로, 코드로 트레드 방법

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's에서 '을 접두사해야합니다

a class="as_Glossary" onmouseover="unescapeHTML('The manufacturer\&#39;s sales... 

당신은 또한 당신의 alert 표현식에서 괄호 필요없는 charachter 사이가

function unescapeHTML(html) { 
    var htmlNode = document.createElement("div"); 
    htmlNode.innerHTML = html; 
    if(htmlNode.innerText) 
    alert(htmlNode.innerText); // IE 
    else 
    alert(htmlNode.textContent); // FF 
} 
+0

차가움. 알았다! 지금 일하는 것 같습니다. 브라이언 감사합니다. – Eqbal