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

数组javascript中未定义的第一个元素,javascript,arrays,ajax,Javascript,Arrays,Ajax,我对javascript/AJAX非常陌生,所以如果这很明显,请不要太苛刻 据我所知(我可能错了),我有一些javascript可以读取xml文件并提取time标记之间的所有内容。 问题是,无论我如何编码,输出的第一个列表项都是未定义的。 有人能解释为什么会发生这种情况以及可能的解决方案吗? 另外,如果还有其他严重错误,请告诉我 我试过了 if(x[0] !== 'undefined'){txt=txt + "<li class=\"result\">" + x[i].firstCh

我对
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>";}
下面是完整的html文件,分为几部分。我已经把它简化为相关的部分

风格与起点

身体
{
文本对齐:居中;
高度:1100px;
背景色:#EAEAEA
}
菜单{
列表样式类型:无;;
填充:0px;
宽度:205px;
高度:500px;
边际:0px;
溢出y:滚动;
溢出x:隐藏;
}
菜单:--webkit滚动条{
显示:无;
}
#按钮容器{
列表样式类型:无;;
填充:0px;
宽度:205px;
高度:500px;
边际:0px;
溢出y:滚动;
溢出x:隐藏;
}
#按钮容器::-webkit滚动条{
显示:无;
}
时代
{
位置:绝对位置;
显示:块;
宽度:35%;
身高:100%;
左:0px;
右边框:2倍实心#E5;
}
李:结果如何
{
显示:块;
字体系列:“Lucida Sans”;
字体大小:12px;
字体大小:粗体;
宽度:164px;
填充顶部:30px;
填充底部:30px;
颜色:#5D5D5D;
文本对齐:居中;
文字装饰:无;
宽度:100%;
边框底部:1px实心#CCCC;
边框顶部:1px实心#中交;
背景色:#EEEEEE;
}
结果:悬停
{
背景色:#6BB9F0;
颜色:#fcfc;
}
.btn{
位置:相对位置;
显示:块;
边界:无;
字体系列:“Lucida Sans”;
字体大小:10px;
字体大小:粗体;
宽度:204px;
填充顶部:20px;
垫底:20px;
颜色:#5D5D5D;
背景色:#FAFAFA;
文本对齐:左对齐;
文字装饰:无;
}
.btn:悬停{
背景色:#5CD8CD;
颜色:#fcfc;
}
脚本部分

函数myFunction(url){
var-xmlhttp;
var-txt,x,i;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
x=xmlhttp.responseXML.documentElement.getElementsByTagName(“时间”);

对于(i=0;i如果其他人有这个问题,我猜这不太可能,因为这是一个愚蠢的错误

未定义的列表项是由于将
txt
添加到行中其自身(以前未定义)时未定义

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

在这行之前修复了问题。

请同时发布XML数据。是否
x[0]
未定义,或
x[0]。firstChild
,或
x[0]。firstChild.nodeValue
?@Alexander抱歉,我想添加,但它现在已经被添加了。@Alexander你知道哪一个是未定义的吗?
<!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>
<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>
 txt=txt + "<li class=\"result\">" + x[i].firstChild.nodeValue + "</li>";
txt=''