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。