我正在尝试将一些xml数据(productType.xml)放入一个html文件(customo.php)我使用javascript(script.js)供用户选择一类产品。
的script.js
window.onload = init;
function init(){
var xml;
if(window.XMLHttpRequest){
xml = new XMLHttpRequest();
}else{
xml = new ActiveXObject("Microsoft.XMLHTTP");
}
xml.open("GET","data/productType.xml",false);
xml.send();
xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("product");
var message;
for(i=0; i<x.length; i++){
message = "<div class='pro'><p>" + x.getElementsByTagName("category")[i].childNodes[0].nodeValue +"</p><img src='"+ x.getElementsByTagName("catimage")[i].childNodes[0].nodeValue + "' alt='Picture' /></div>";
}
document.getElementById("show1").innerHTML = message;
}
productType.xml
<?xml version="1.0" encoding="UTF-8" ?>
<products>
<product>
<category>Pens</category>
<catimage>d-u-b/ball1.png</catimage>
<types>
<one>Laser</one>
<two>Plain</two>
</types>
<colors>
<one>Pink</one>
<two>Red</two>
<three>Blue</three>
<four>Black</four>
</colors>
<price>2.99</price>
</product>
<product>
<category>Stress Balls</category>
<catimage>d-u-b/ball1.png</catimage>
<types>
<one>Round</one>
<two>Custom</two>
</types>
<images>
<one>d-u-b/ball1.png</one>
<two>d-u-b/custom.png</two>
</images>
<colors>
<one>Green</one>
<two>Red</two>
<three>Blue</three>
<four>Light Blue</four>
</colors>
<imagecolors>
<one>d-u-b/ballgreen.png</one>
<two>d-u-b/ballred.png</two>
<three>d-u-b/ballblue.png</three>
<four>d-u-b/balllightblue.png</four>
</imagecolors>
<price>1.99</price>
</product>
</products>
我遇到此错误&#34;未捕获的TypeError:无法读取属性&#39; getElementsByTagName&#39; null init&#34; script.js 14
第14行
var x = xmlDoc.getElementsByTagName("product");
我已经尝试过调试,它通过xmlDoc = xml.responseXML;
有人能看到最新情况吗?