XML DOM appendChild() পদ্ধতি
সংজ্ঞা ও ব্যবহার
appendChild()
মথক নতুন সাব-নোডকে নোডের সাব-নোড তালিকার শেষে যুক্ত করে。
মন্তব্য:যদি newchild তার গাছে সবাই থাকলে, তাকে প্রথমে মুক্ত করা হবে。
গ্রামাটিকা
nodeObject.appendChild(newchild)
পারামিটার | বর্ণনা |
---|---|
newchild | নিবন্ধিত নোড。 |
তকনীকী বিবরণ
DOM সংস্করণ: | কোর লেভেল 1 নোড অবজেক্ট। DOM লেভেল 3-তে সংশোধিত。 |
---|---|
ফলাফল: | নোড অবজেক্ট। যুক্ত করা নোড。 |
ইনস্ট্যান্স
উদাহরণ 1
এই কোড "books.xml"-কে xmlDoc-তে লোড করে, একটি নোড (<edition>) তৈরি করে, এবং তা প্রথম <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 xmlDoc = xml.responseXML; var newel = xmlDoc.createElement("edition"); var x = xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel); document.getElementById("demo").innerHTML = x.getElementsByTagName("edition")[0].nodeName; }
Example 2
Append child nodes to all <book> nodes:
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, y, z, i, xLen, yLen, newEle, newText, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book"); xLen = x.length; for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("first"); newEle.appendChild(newText); x[i].appendChild(newEle); } // Output all title and edition y = xmlDoc.getElementsByTagName("title"); yLen = y.length z = xmlDoc.getElementsByTagName("edition"); for (i = 0; i < yLen; i++) { txt += y[i].childNodes[0].nodeValue + " - Edition: " + z[i].childNodes[0].nodeValue + "<br>"; } document.getElementById("demo").innerHTML = txt; }
浏览器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |
所有主流浏览器都支持 appendChild() 方法。