郁闷
部分代码
- HTML code
function listAllStates() { var xmlDoc=xmlHttp.responseXML; var allStates=xmlDoc.getElementsByTagName("state"); outputList("All States in Document",allStates); } function outputList(title,states) { var out=title; var currentState=null; for(var i=0;i<states.length;i++) { currentState=states[i]; out=out+"\n-"+currentState.childNodes[0].nodeValue; }alert(out); }
------解决方案--------------------
下面的代码已经测试过。It works.
- JScript code
function listAllStates() { var xmlDoc; var xmlString = '<states><state>wei</state></states>'; // Mozilla and Netscape browsers if (document.implementation.createDocument) { var parser = new DOMParser() xmlDoc = parser.parseFromString(xmlString, "application/xml") // MSIE } else if (window.ActiveXObject) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.loadXML(xmlString) } var x = xmlDoc.getElementsByTagName('state'); alert (xmlDoc); alert (x.length); } listAllStates();
------解决方案--------------------
你保证你的远程XML读取正确么?
还有是否可以考虑使用XPATH来解决
也就是XDOC.selectNodes("//state").length;
------解决方案--------------------
- JScript code
function handleStateChange() { if(xmlHttp.readyState==4){ //如果你直接拖进浏览器的话正确执行时返回的状态为0,要想返回200状态需要服务器支持 //所以要加上xmlHttp.status==0的判断 if(xmlHttp.status==200||xmlHttp.status==0){ if(requestType=="north"){ listNothStates(); } else if(requestType=="all"){ listAllStates(); } } } }