数组javascript中未定义的第一个元素

时间:2014-10-06 13:18:37

标签: javascript arrays ajax

我对javascript / AJAX不熟悉,所以如果这很明显,请不要过于苛刻。

据我所知(我可能错了)我有一些javascript读取xml文件并提取time标签之间的所有内容。
问题是,无论我如何编码它,输出的第一个列表项都是未定义的 有人能够解释为什么会这样,请问可能的解决方案吗? 如果其他任何事情严重错误,请告诉我

我试过了

if(x[0] !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";}
if(x !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";}
if(x[0].firstChild.nodeValue !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";}
if(x[i].firstChild.nodeValue !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";}
if(x[i].firstChild.nodeValue !== ''){txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";}

还尝试为第一个元素分配内容

x[0]="n";

将完整的html拆分成碎片。我把它简化为相关部分。

样式并开始

<!DOCTYPE html>
<html>
<head>
<style>
body
{
    text-align: center;
    height: 1100px;
        background-color: #EAEAEA
}


ul.menu {
    list-style-type: none;;
        padding: 0px;
        width: 205px;
        height: 500px;
        margin: 0px;
        overflow-y: scroll;
        overflow-x: hidden;


}

ul.menu::-webkit-scrollbar {
  display: none;
}
#button-container {
    list-style-type: none;;
    padding: 0px;
    width: 205px;
    height: 500px;
    margin: 0px;
    overflow-y: scroll;
        overflow-x: hidden;


}

#button-container::-webkit-scrollbar {
  display: none;
}

times
{

    position: absolute;
    display: block;
    width: 35%;
    height: 100%;
    left:0px;
    border-right: 2px solid #E5E5E5;
}

li.result
{
    display: block;
    font-family: "Lucida Sans";
    font-size: 12px;
    font-weight: bold;
    width:164px;
    padding-top: 30px;
    padding-bottom: 30px;
    color: #5D5D5D;
    text-align: center;
    text-decoration: none;
    width:100%;
    border-bottom: 1px solid #CCCCCC;
    border-top: 1px solid #CCCCCC;
    background-color: #EEEEEE;

}

li.result:hover
{
    background-color: #6BB9F0;
    color: #FCFCFC;
}

.btn {
  position:relative;
  display:block;
  border:none;
  font-family: "Lucida Sans";
  font-size: 10px;
  font-weight: bold;
  width:204px;
  padding-top: 20px;
  padding-bottom: 20px;
  color: #5D5D5D;
  background-color: #FAFAFA;
  text-align: left;
  text-decoration: none;
}
.btn:hover{
    background-color: #5CD8CD;
    color: #FCFCFC;
}



</style>

脚本部分

<script type="text/javascript">

                        function myFunction(url) {
var xmlhttp;
var txt,x,i;
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)
    {
    x=xmlhttp.responseXML.documentElement.getElementsByTagName("time");
    for (i=0;i<x.length;i++)
        {
          txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";
    }

    document.getElementById('bob').innerHTML=txt;
    }
  }
xmlhttp.open("GET",url,true);
xmlhttp.send();

}
    </script>
</head>

(剥离了这个)。

<body>
                        <button class="btn" style="text-align:center;"  onclick="myFunction('test.xml')">ALL</button>
                    <times>
                            <ul id="bob" class="menu" style="width:100%;height:91%;position:relative;top:29px;border-top: 1px solid #CCCCCC;">
                                </ul>
                    </times>


</body>
</html>

编辑:

XML

<root>
<time>Mon Sep 29 11:20:00 BST 2014</time>
<time>Mon Sep 29 11:20:15 BST 2014</time>
<time>Mon Sep 29 11:20:30 BST 2014</time>
<time>Mon Sep 29 11:20:45 BST 2014</time>
<time>Mon Sep 29 11:21:00 BST 2014</time>
<time>Mon Sep 29 11:21:15 BST 2014</time>
<time>Mon Sep 29 11:21:30 BST 2014</time>
<time>Mon Sep 29 11:21:45 BST 2014</time>
<time>Mon Sep 29 11:22:00 BST 2014</time>
<time>Mon Sep 29 11:22:15 BST 2014</time>
<time>Mon Sep 29 11:22:30 BST 2014</time>
<time>Mon Sep 29 11:22:45 BST 2014</time>
<time>Mon Sep 29 11:23:00 BST 2014</time>
<time>Mon Sep 29 11:23:15 BST 2014</time>
<time>Mon Sep 29 11:23:30 BST 2014</time>
<time>Mon Sep 29 11:23:45 BST 2014</time>
<time>Mon Sep 29 11:24:00 BST 2014</time>
<time>Mon Sep 29 11:24:15 BST 2014</time>
<time>Mon Sep 29 11:24:30 BST 2014</time>
<time>Mon Sep 29 11:24:45 BST 2014</time>
<time>Mon Sep 29 11:25:00 BST 2014</time>
<time>Mon Sep 29 11:25:15 BST 2014</time>
<time>Mon Sep 29 11:25:30 BST 2014</time>
<time>Mon Sep 29 11:25:45 BST 2014</time>
<time>Mon Sep 29 11:26:00 BST 2014</time>
<time>Mon Sep 29 11:26:15 BST 2014</time>
<time>Mon Sep 29 11:26:30 BST 2014</time>
<time>Mon Sep 29 11:26:45 BST 2014</time>
<time>Mon Sep 29 11:27:00 BST 2014</time>
<time>Mon Sep 29 11:27:15 BST 2014</time>
<time>Mon Sep 29 11:27:30 BST 2014</time>
<time>Mon Sep 29 11:27:45 BST 2014</time>
</root>

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

如果其他人有这个问题,我猜这是不可能的,因为这是一个愚蠢的错误

未定义的列表项是由于txt未被定义,因为它被添加到行中的(以前未定义的)自我

 txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";

添加

txt=''

此行之前解决了问题。