2012-09-28 29 views
-1

我试图使用loadXMLDoc()JavaScript函数从XML文档加载数据,并在单击按钮时将其显示在我的页面DIV上。到目前为止,我无法在我的DIV中获得任何内容,我想加载特定标记名中的所有元素。使用JavaScript显示标记名内的所有节点

这里的XML:

<?xml version="1.0" encoding="UTF-8"?> 
<Fruits> 
    <Cell>Apples</Cell> 
    <Cell>Bananas</Cell> 
    <Cell>Strawberries</Cell> 
</Fruits> 
<Vegetables> 
    <Cell>Lettuce</Cell> 
    <Cell>Tomatoes</Cell> 
    <Cell>Carrots</Cell> 
</Vegetables> 

这里的JavaScript的:

function fruits() 
{ 
    var 
    xmlhttp; 
    if (window.XMLHttpRequest) 

    {// code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else 
    {// code for IE6, IE5 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      xmlDoc = xmlhttp.responseXML; 
      document.getElementById("food").innerHTML=xmlDoc.getElementsByTagName("fruits"); 
     } 
     xmlhttp.open("GET","document.xml", true); 
     xmlhttp.send(); 
    } 

    function vegetables() 
    { 
     var 
     xmlhttp; 
     if (window.XMLHttpRequest) 

     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      xmlDoc = xmlhttp.responseXML; 
      document.getElementById("food").innerHTML=xmlDoc.getElementsByTagName("vegetables"); 
     } 
     xmlhttp.open("GET","document.xml", true); 
     xmlhttp.send(); 
    } 

而这里的HTML:

<button type="button" onclick="fruits()">Fruits</button> 
<button type="button" onclick="vegetables()">Vegetables</button> 
<div id="food">Please select your favorite</div> 

回答

1

你的功能fruits()无法正常关闭,并且你的函数vegetables()已经结束了功能fruits()

也是如此定义函数xmlhttp.onreadystatechange=function()

所以这些功能甚至不会使用,直到fruits()被调用。

您的代码应该是这样的:

function fruits() 
    { 
     var 
     xmlhttp; 
     if (window.XMLHttpRequest) 

     {// code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     {// code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       xmlDoc = xmlhttp.responseXML; 
       document.getElementById("food").innerHTML=xmlDoc.getElementsByTagName("fruits"); 
      } 
     } 

     //something went wrong here : this code ended up in the function that was to be called when xmlhttp was done with its GET operation. 
     //consequently it was never called 
     xmlhttp.open("GET","document.xml", true); 
     xmlhttp.send(); 
    } // <-- this here bracket was missing 

    function vegetables() 
    { 
     var 
     xmlhttp; 
     if (window.XMLHttpRequest) 
     { 
      // code for IE7+, Firefox, Chrome, Opera, Safari 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else 
     { 
      // code for IE6, IE5 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange=function() 
     { 
      if (xmlhttp.readyState==4 && xmlhttp.status==200) 
      { 
       xmlDoc = xmlhttp.responseXML; 
       document.getElementById("food").innerHTML=xmlDoc.getElementsByTagName("vegetables"); 
      } 
     } 

     //the same thing happened here 

     xmlhttp.open("GET","document.xml", true); 
     xmlhttp.send(); 
    } 
+0

谢谢提摩太,我在新的编码,可以请你给我一个例子,我怎么能解决这个问题吗?非常感激! – Ken

+0

你好,欢迎来到StackOverflow。 我为您更新了答案,它现在包含代码,因为它可能应该是,并且对某些事情出错的地方附带一些注释注释。 为了防止将来发生类似情况,请密切关注代码格式,缩进是一种很好的工具,可以防止出现这种情况,并且语法高亮显示编辑器也可以提供很多帮助! 如果我的回答对你有帮助,不要忘记加注,或者如果它解决了你的问题,一定要把它标记为可接受的答案。 快乐编码! –

+0

嗨蒂莫西,谢谢你的回答,但不幸的是仍然没有工作...什么都没有显示出来......我只用水果尝试过,仍然...标记名(“水果”)内没有任何元素显示:-(,I无法找到加载此信息的方式,请有人帮助我,谢谢 – Ken

相关问题