วิธี XML DOM replaceData()

คำอธิบายและใช้งาน

replaceData() วิธีแทนที่ข้อมูลในตัวเลือกของความคิดเห็น

ภาษา

commentNode.replaceData(start,length,string)
ตัวแปร รายละเอียด
start ว่าด้วยความบำเหน็จ
length ว่าด้วยความบำเหน็จ
string ว่าด้วยความบำเหน็จ

ตัวอย่าง

รหัสที่แต่งตั้งด้านล่างนี้จะนำ "books_comment.xml" โหลดเข้าไปยัง xmlDoc และแทนที่ "Simple" ด้วย "Easy" ในข้อความที่เป็นความคิดเห็นของตัวเลือก <book> แรก

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   if (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books_comment.xml", true);
xhttp.send();
function myFunction(xml) {
    var x, i, txt, xmlDoc;
    xmlDoc = xml.responseXML;
   txt = "";
    x = xmlDoc.getElementsByTagName("book")[0].childNodes;
    for (i = 0; i < x.length; i++) {
    // จัดการเฉพาะตัวเลือกโครงสร้างของความคิดเห็น
        if (x[i].nodeType == 8) {
            x[i].replaceData(4, 6, "Easy");
            txt += x[i].data + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt;
}

ทดลองด้วยตัวเอง

ในตัวอย่างที่นี้ เราใช้วนระบบและคำสั่ง if ตรวจสอบเพื่อที่จะเป็นไปตามมาเฉพาะจุดประสงค์ของตัวเลือกหมายเลข 8