2012-05-24 26 views
0

我有以下的HTML代碼顯示的變量值:Chrome和IE不會從XML文件

<html> 
<body> 
<h1>W3Schools Internal Note</h1> 
<div> 
<b>To:</b> <span id="to"></span><br /> 
<b>From:</b> <span id="from"></span><br /> 
<b>Message:</b> <span id="message"></span> 
</div> 

<script type="text/javascript"> 
if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else 
{// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.open("GET","languages.xml",false); 
xmlhttp.send(); 
xmlDoc=xmlhttp.responseXML; 

document.getElementById("to").innerHTML= 
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue; 
document.getElementById("from").innerHTML= 
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue; 
document.getElementById("message").innerHTML= 
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue; 
</script> 

</body> 
</html> 

此代碼在Firefox,但在Chrome和IE瀏覽器無法正常工作。我的意思是Firefox顯示來自XML文檔的XML標籤的結果,但Chrome和IE不是! 請幫我一把。

+0

1)同步AJAX調用是一個壞主意。 2)停止使用w3schools:http://www.w3fools.com – DCoder

回答

0

嘗試在所有元素上用textContent代替nodeValue。如果這不是解決方案,請發佈您的XML。

相關問題