XML DOM textContent özniteliği
Tanım ve Kullanım
textContent
Öznitelik ayarlar veya döner ve nodun kendisi ile soyu olan metin içeriğini.
Ayarlandığında, tüm alt nodlar silinir ve bu öznitelik değerini içeren tek bir metin nodu ile değiştirilir.
Gramer
nodeObject.textContent
Örnek
Örnek 1
Aşağıdaki kod, "books.xml" dosyasını xmlDoc'a yükler ve <book> öğesinin metin içeriğini döner:
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; }
Örnek 2
Nodun metin içeriğini ayarla:
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 ayarı for(i = 0; i < x.length; i++) { x.item(i).textContent = "Eskiden"; } // textContent çıktısı yap for(i = 0; i < x.length; i++) { txt += x.item(i).textContent + "<br>"; } document.getElementById("demo").innerHTML = txt; }