简体   繁体   中英

Update HTML Page using XML and Javascript

I am trying to update a website using AJAX.

In my html I used the span id="test1" and span id="test2" .

However only the content of test1 is showed and not the content of test2 from the XML file. Can somebody tell me what I am doing wrong in my javascript below?

window.onload = function ShowAlert() {
    if(window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
         // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET", "note.xml", false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML;
    document.getElementById("test1").innerHTML = xmlDoc.getElementsByTagName("test1")[0].childNodes[0].nodeValue;
    document.getElementById("test2").innerHTML = xmlDoc.getElementsByTagName("test2")[0].childNodes[0].nodeValue;
}

由于javascript中的错误不会突然传给我,因此我建议您使用检查器(例如DevTools)来检查span id="test2"也许还有其他错误(除了javascript,可能是DOM结构)导致了数据不出现。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM