2014-10-06 4 views
0

나는 매우 javascript/AJAX에 익숙하다. 그렇다면 너무 가혹하지 마십시오.배열 자바 스크립트에서 정의되지 않은 첫 번째 요소

내가 아는 한 (내가 틀릴 수도 있음) 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"; 

조각으로 Heres는 전체 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> 

감사

+2

XML 데이터도 게시하십시오. 'x [0]'이 정의되지 않았거나'x [0] .firstChild' 또는'x [0] .firstChild.nodeValue'입니까? – Alexander

+0

@Alexander 미안하지만 나는 그것을 추가하려했는데, 지금 추가되었습니다. –

+0

@Alexander 어느 것이 정의되지 않았는지 아십니까? –

답변

1

다른 사람은 바보 같은 실수

있다는 가능성이 내가 추측하고이 문제가있는 경우,

정의되지 않은 목록 항목은 txt이 t이 라인이 문제를 해결

txt='' 

이전에 추가 라인

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

년 (이전에 정의되지 않은) 자체에 추가됩니다.

관련 문제