XML DOM substringData() 方法
❮ 註釋物件
示例
以下程式碼片段載入 "books_comment.xml" 到 xmlDoc,並從第一個註釋元素中獲取 "(Hardcover)" 字串。
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, y, txt, xmlDoc;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName("book")[0].childNodes;
for (i = 0; i < x.length; i++) {
// 只處理註釋節點
if (x[i].nodeType == 8) {
y = x[i].substringData(33, 11);
txt += y + "<br>";
}
}
document.getElementById("demo").innerHTML = txt;
}
輸出
(Hardcover)
自己動手試一試 »
在上面的示例中,我們使用了迴圈和 if 測試來確保我們只處理註釋節點。註釋節點的 nodeType 為 8。
定義和用法
substringData() 方法從註釋節點中獲取字串。
語法
substringData(start,length)
引數 | 描述 |
---|---|
start | 必需。指定開始提取字元的位置。Start 值從零開始。 |
length | 必需。指定要提取的字元數。 |
❮ 註釋物件