XML DOM createTextNode() روش

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

createTextNode() این روش یک گره متن ایجاد می‌کند.

این روش یک عنصر Text را برمی‌گرداند.

زبان نوشتاری

createTextNode(text)
پارامتر توضیح
text زیر نویسی، متن گره را مشخص می‌کند.

مثال

این کد "books.xml" را به xmlDoc بارگذاری می‌کند و به هر عناصر <book> یک گره عناصر با گره متن حاوی می‌افزاید:

ویرایشگر xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
   اگر (this.readyState == 4 && this.status == 200) {
       myFunction(this);
   }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
دستور العمل myFunction(xml) {
    ویرایشگر x, y, z, i, xLen, yLen, newEle, newText, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    xLen = x.length;
    // ایجاد گره‌های عناصر و گره‌های متن
    برای (i = 0; i < xLen; i++) {
        newEle = xmlDoc.createElement("edition");
        newText = xmlDoc.createTextNode("first");
        newEle.appendChild(newText);
        x[i].appendChild(newEle);
    }
    // همه title و edition را چاپ کنید
    y = xmlDoc.getElementsByTagName("title");
    yLen = y.length
    z = xmlDoc.getElementsByTagName("edition");
    برای (i = 0; i < yLen; i++) {
        txt += y[i].childNodes[0].nodeValue +
        " - Edition: " +
       z[i].childNodes[0].nodeValue + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

آزمایش کنید