日期:2014-05-16  浏览次数:20667 次

狂郁闷 我的ajax第三个例子就把我给搞死了 主要问题 getElementsByTagName返回总是0 - Web 开发 / Ajax
parseXML.html
HTML code
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>paesing XML Response with the w3c DOM </title>
<script type="text/javascript">
var xmlHttp;
var requestType="";
function createXMLHttpRequest()
{
  if(window.ActiveXObject){
      xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
}
  else if(window.XMLHttpRequest){
      xmlHttp=new XMLHttpRequest();
}
}
function startRequest(requestedList)
{
  requestType=requestedList;
  createXMLHttpRequest();
  xmlHttp.onreadystatechange=handleStateChange;
  xmlHttp.open("GET","parseXML.xml",true);
  xmlHttp.send(null);
}
function handleStateChange()
{
  if(xmlHttp.readyState==4){
        if(xmlHttp.status==200){
          if(requestType=="north"){
    listNothStates();
  }
  else if(requestType=="all"){
    listAllStates();
          }
    }
  }
}
function listNorthStates()
{
  var xmlDoc=xmlHttp.responseXML;
  var northNode=xmlDoc.getElementsByTagName("north")[0];
  var out="Northern States";
  var northStates=northNode.getElementsByTagName("state");
  outputList("Northern States ",northStates);
}
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);
}
</script>
</head>

<body>
<h1>process xml document of u.s. states </h1>
<form action="#">
<br /> <br />
<input type="button" value="view all listed states"
  onclick="startRequest('all');" />
<br /> <br />
<input type="button" value="view all listed north northern states"
  onclick="startRequest('north');" />
  </form>
</body>
</html>


parseXML.xml
XML code
<?xml version="1.0" encoding="UTF-8"?>
<states>
    <north>
        <state>Minnesota</state>
        <state>Iowa</state>
        <state>North Dakota</state>
    </north>
    <south>
        <state>Texas</state>
        <state>Oklahoma</state>
        <state>Louisiana</state>
    </south>
    <east>