我有一個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