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; }