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; }
例子 2
将子节点追加到所有 <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, 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); } // 输出所有 title 和 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() 方法。