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 | 必需。指定要提取的字符数。 |
❮ 注释对象