سؤال

لدي استدعاء طريقة 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>

عندما أنا الفحلي أحصل على الخطأ

هل كانت مفيدة؟

المحلول

المشكلة هي أن الخاص بك &#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, ، والباقي مدقت كود، مع اقتباس وثيق إضافي لا مثيل له '. وبعد تحتاج إلى بادئة ' في 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
}

نصائح أخرى

تحتاج إلى أ فاصلة منقوطة بعد تلك المرجع الشخصية

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top