我有一个XML文件:计数节点的数量不工作
<?xml version="1.0" encoding="ISO-8859-1"?>
<food>
<cuisine type="Chinese">
<restaurant name = "Panda Express">
<location id= "0"></location>
<phone id = "1"></phone>
<city id="2"></phone>
</restaurant>
<restaurant name = "Mr. Chau's">
</restaurant>
</cuisine>
<cuisine type="Indian">
<restaurant name = "Shan">
</restaurant>
</cuisine>
</food>
,我试图来算这个美食节点的数量是我的代码,我知道它基本上是正确的,但是当我尝试打印出它说,它的0
//starts talking to the xml document
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","data.xml", false);
xmlhttp.send();
xmlData = xmlhttp.responseXML;
//fills the first comboBox
var sel = document.getElementById('CuisineList');
cuisineList = xmlData.getElementsByTagName('cuisine');
document.getElementById("test").innerHTML = cuisineList.length;
强烈建议**不要**做同步ajax请求(即'false'作为'open'的第三个参数使其同步);改为使用*异步*请求和'onreadystatechangehandler'回调。同步ajax请求会导致糟糕的用户体验。 – 2012-07-07 07:40:12