| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1106 人关注过本帖
标题:ajax中的getElementByTagName("..").length返回为0 狂郁闷
取消只看楼主 加入收藏
zmhdxy
Rank: 1
等 级:新手上路
帖 子:452
专家分:0
注 册:2007-9-27
收藏
 问题点数:0 回复次数:3 
ajax中的getElementByTagName("..").length返回为0 狂郁闷
parseXML.html
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. 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>
搜索更多相关主题的帖子: ajax length 
2008-05-11 16:49
zmhdxy
Rank: 1
等 级:新手上路
帖 子:452
专家分:0
注 册:2007-9-27
收藏
得分:0 
程序代码:
parseXML.xml 
<?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>
        <state>New York</state>
        <state>North Carolina</state>
        <state>Massachusetts</state>
    </east>
    <west>
        <state>California</state>
        <state>Oregon</state>
        <state>Nevada</state>
    </west>
</states>

2008-05-11 16:50
zmhdxy
Rank: 1
等 级:新手上路
帖 子:452
专家分:0
注 册:2007-9-27
收藏
得分:0 
代码是多了点 不过是基础的ajax 大家帮忙了

2008-05-11 22:27
zmhdxy
Rank: 1
等 级:新手上路
帖 子:452
专家分:0
注 册:2007-9-27
收藏
得分:0 
问题解决 xml中的编码出现问题
还有代码中的有个listNothStates();
写错了
因该是listNorthSrtates();

2008-05-12 11:13
快速回复:ajax中的getElementByTagName("..").length返回为0 狂郁闷
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.047250 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved