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
❮ 注释对象