XML DOM insertData() 方法
❮ 註釋物件
示例
以下程式碼片段將“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;
}
輸出
125 份簡單美味的義大利食譜(精裝版)
自己動手試一試 »
在上面的示例中,我們使用了迴圈和 if 測試來確保我們只處理註釋節點。註釋節點的 nodeType 為 8。
定義和用法
insertData() 方法向註釋節點插入資料。
語法
commentNode.insertData(start,string)
引數 | 描述 |
---|---|
start | 必需。指定開始插入字元的位置。Start 值從零開始 |
string | 必需。指定要插入的字串 |
❮ 註釋物件