<script>
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","Default.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var management=xmlDoc.getElementsByTagName("Management");
document.write("<tr><td>");
document.write(management[i].getElementsByTagName("RepresentativeName")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
</script>
我正在其他浏览器中使用它,但 xml 元素中没有任何值。但 ie 向我抛出错误并且不渲染 UI
最佳答案
在将其写入文档之前,您需要添加 if 条件。
尝试这样的代码
<script>
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","Default.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var management=xmlDoc.getElementsByTagName("Management");
document.write("<tr><td>");
if(management[i].getElementsByTagName("RepresentativeName")[0].childNodes[0]){
document.write(management[i].getElementsByTagName("RepresentativeName")[0].childNodes[0].nodeValue);
}else{
document.write('');
}
document.write("</td></tr>");
</script>
关于javascript - 无法获取属性值 'nodeValue' : object is null or undefined in IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31404212/