XML DOM createElement() روش
تعریف و استفاده
createElement()
این روش یک نود عناصر ایجاد میکند.
این روش یک عنصر Element را برمیگرداند.
نحوهی نوشتن
createElement(name)
پارامترها | توضیح |
---|---|
name | زبان، تعریف نام عناصر. |
مثال
این کد "books.xml" را به xmlDoc بارگذاری میکند و به هر عناصر <book> یک نود عناصر با نود متن حاوی میافزاید:
واردات var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { اگر (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); دستور کار myFunction(xml) { واردات var 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; }