2015-10-21 297 views
1

一個常見的問題,但嚴重爲什麼我的循環不工作?因爲我已什麼錯誤,任何幫助,將不勝感激:)JavaScript for循環不循環

XML

<?xml version="1.0" encoding="utf-8"?> 
<School> 
    <child> 
    <name>john</name> 
    <dob>28-09-2000</dob> 
    <age>15</age> 
    <add>jgdkhcjgvbv</add> 
    <phone>12345678</phone> 
    </child> 
    <child> 
    <name>henry</name> 
    <dob>19-09-2001</dob> 
    <age>14</age> 
    <add>kjdfndsjnlae</add> 
    <phone>23456789</phone> 
    </child> 
    <child> 
    <name>luke</name> 
    <dob>27-04-1999</dob> 
    <age>16</age> 
    <add>ksmfodnzsksl</add> 
    <phone>34567890</phone> 
    </child> 
</School> 

HTML

<html> 
<head> 
<script type="text/javascript" > 
    var xmlObject, docObject, childarray, cname, cdob, cage; 
    xmlObject = new XMLHttpRequest(); 
    xmlObject.open("GET","school.xml",false); 
    xmlObject.send(); 
    docObject = xmlObject.responseXML; 
    childarray=docObject.getElementsByTagName("child"); 

    for (var i = 0; i<childarray.length; i++) { 
     cname = docObject.getElementsByTagName("name")[i].childNodes[0].nodeValue; 
     document.write("Name : " + cname + "<BR/>"); 

     cdob = docObject.getElementsByTagName("dob")[i].childNodes[0].nodeValue; 
     document.write("Dob : " + cdob + "<BR/>"); 

     cage = docObject.getElementsByTagName("age")[i].childNodes[0].nodeValue; 
     document.write("Age : " + cage + "<BR/>"); 

     cadd = docObject.getElementsByTagName("add")[i].childNodes[0].nodeValue; 
     document.write("Add : " + cadd + "<BR/>"); 

     cphone = docpb.getElementsByTagName("phone")[i].childNodes[0].nodeValue; 
     document.write("Phone : " + cphone + "<BR/>"); 

    } 
</script> 
<title>school</title> 
</head> 
<body> 
</body> 
</html> 

我想要做的是打印的姓名,出生日期,年齡,地址,一次給'n'個學生打電話。該代碼僅顯示第一個孩子的詳細信息。

韋爾普

+0

您當前的輸出表明'childarray'的長度爲1,因此您必須確定爲什麼會出現這種情況。 –

回答

0

您是否嘗試過 「=」,在

VAR添加I = 0;我< = childarray.length; i ++