XML DOM appendData() 方法

定义和用法

appendData() 方法在注释节点的末尾添加数据。

语法

commentNode.appedData(string)
参数 描述
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++) {
    // 只处理评论节点Process only comment nodes
        if (x[i].nodeType == 8) {
            x[i].appendData(" Special Offer");
            txt += x[i].data + "<br>";
        }
    }
    document.getElementById("demo").innerHTML = txt;
}

亲自试一试

在上面的例子中,我们用了循环和 if 测试语句,来确保我们只处理注释节点。注释节点的节点类型为 8。