ویژگی 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');
    برای(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
    برای(i = 0; i < x.length; i++) {
        x.item(i).textContent = "خردہتا";
    }
    // خروج textContent
    برای(i = 0; i < x.length; i++) {
        txt += x.item(i).textContent + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

آزمایش کنید