XML DOM textContent 属性
定义和用法
textContent 属性设置或返回节点及其后代的文本内容。
当设置时,所有子节点都将被删除并替换为包含此属性值的单个文本节点。
语法
nodeObject.textContent
实例
例子 1
下面的代码将 "books.xml" 加载到 xmlDoc 中,并返回 <book> 元素的文本内容:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
for(i = 0; i < x.length; i++) {
txt += x.item(i).textContent + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
例子 2
设置节点的文本内容:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
}
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('book');
// 设置 textContent
for(i = 0; i < x.length; i++) {
x.item(i).textContent = "Outdated";
}
// 输出 textContent
for(i = 0; i < x.length; i++) {
txt += x.item(i).textContent + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}