خصائص textContent DOM XML

التعريف والاستخدام

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

تجربة شخصية