XML DOM textContent ominaisuus

Määrittely ja käyttö

textContent Ominaisuus asettaa tai palauttaa solmun ja sen jälkeläisten tekstitiedot.

Kun asetetaan, kaikki alijohdot poistetaan ja korvataan yhdellä tekstityypin solmulla, joka sisältää tämän ominaisuuden arvon.

Syntaksi

nodeObject.textContent

Esimerkki

Esimerkki 1

Seuraava koodi ladataan "books.xml" xmlDoc:iin ja palauttaa <book>-elementin tekstitiedot:

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

Kokeile itse

Esimerkki 2

Aseta solmun tekstitiedot:

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');
    // Aseta textContent
    for(i = 0; i < x.length; i++) {
        x.item(i).textContent = "Outdated";
    }
    // Tulosta textContent
    for(i = 0; i < x.length; i++) {
        txt += x.item(i).textContent + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

Kokeile itse