طريقة appendChild() لـ XML DOM
التعريف والاستخدام
appendChild()
يضيف الطريقة عقدة بعد آخر فرع لعقدة العنصر المحدد.
يستعيد هذا الطريقة عقدة فرع جديدة.
النحو
appendChild(العقدة)
الم 参数 | الوصف |
---|---|
العقدة | مطلوب. العقدة التي سيتم إضافتها. |
مثال
مثال 1
الكود التالي يقوم بتحميل "books.xml" إلى xmlDoc، وإنشاء عقدة (<edition>)، ثم إضافة هذه العقدة بعد آخر فرع لعقدة <book> الأولى:
هناك xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = وظيفة() { إذا (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); وظيفة myFunction(xml) { هناك xmlDoc = xml.responseXML; هناك newel = xmlDoc.createElement("edition"); هناك x = xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); document.getElementById("demo").innerHTML = x.getElementsByTagName("edition")[0].nodeName; }
مثال 2
الكود التالي سيدخل "books.xml" إلى xmlDoc ويضيف العقد الجديدة إلى جميع عناصر <book>:
هناك xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = وظيفة() { إذا (xhttp.readyState == 4 && xhttp.status == 200) { myFunction(xhttp); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); وظيفة myFunction(xml) { هناك x, y, z, i, newel, newtext, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book"); لـ(i = 0; i < x.طول; i++) { newel = xmlDoc.createElement("edition"); newtext = xmlDoc.createTextNode("first"); newel.appendChild(newtext); x[i].appendChild(newel); } // طبع جميع title و edition y = xmlDoc.getElementsByTagName("title"); z = xmlDoc.getElementsByTagName("edition"); لـ(i = 0; i < y.طول; i++) { txt += y[i].childNodes[0].nodeValue + " - Edition: " + z[i].childNodes[0].nodeValue + "<br>"; } document.getElementById("demo").innerHTML = txt; }