ویژگی textContent XML DOM

تعریف و نحوه استفاده

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

亲自试一试