XML DOM appendData() 方法
❮ 註釋物件
示例
下面的程式碼片段將“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].appendData(" 特別優惠");
txt += x[i].data + "<br>";
}
}
document.getElementById("demo").innerHTML = txt;
}
輸出
125 簡單美味的食譜 (精裝) 特別優惠
自己動手試一試 »
在上面的示例中,我們使用了迴圈和 if 測試來確保我們只處理註釋節點。註釋節點的 nodeType 為 8。
定義和用法
appendData() 方法將資料新增到註釋節點的末尾。
語法
commentNode.appedData(string)
引數 | 描述 |
---|---|
string | 必需。要新增到註釋節點的字串 |
❮ 註釋物件