XML DOM data Property
❮ 註釋物件
示例
下面的程式碼片段將 "books_comment.xml" 載入到 xmlDoc 中,並輸出第一個 <book> 元素的 comment 文字
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) {
txt += x[i].data + "<br>";
}
}
document.getElementById("demo").innerHTML = txt;
}
上面程式碼的輸出將是
125 簡單美味的食譜(精裝本)
自己動手試一試 »
在上面的示例中,我們使用了迴圈和 if 測試來確保我們只處理註釋節點。註釋節點的 nodeType 為 8。
定義和用法
data 屬性設定或返回 comment 的文字。
語法
commentNode.data
❮ 註釋物件