เมธอด insertData() ของ XML DOM
การระบุและวิธีใช้
insertData()
วิธีใช้และการใส่ข้อมูลในโคมเมนต์เมนู
รูปแบบ
commentNode.insertData(start,string)
ตัวแปร | รายละเอียด |
---|---|
start | จำเป็น กำหนดจุดเริ่มต้นของการใส่ ค่าเริ่มต้นเป็น 0 |
string | จำเป็น กำหนดข้อความที่ต้องการใส่ |
ตัวอย่าง
รหัสใต้นี้จะนำ "books_comment.xml" โหลดเข้า xmlDoc และใส่ข้อความเข้าไปในโคมเมนต์หลักที่หนึ่ง:
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, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book")[0].childNodes; for (i = 0; i < x.length; i++) { // รับประมาณ แค่โคมเมนต์เมนู if (x[i].nodeType == 8) { x[i].insertData(25, "Italian "); txt += x[i].data + "<br>"; } } document.getElementById("demo").innerHTML = txt; }
在上述例子中,我們用了循環和 if 測試語句,來確保我們只處理注釋節點。注釋節點的節點類型為 8。