XML DOM removeChild() 方法
定義和用法
removeChild()
方法刪除子節點。
成功時,該函數返回被刪除的節點,失敗時返回 NULL
。
語法
elementNode.removeChild(node)
參數 | 描述 |
---|---|
node | 必需。規定要刪除的子節點。 |
實例
例子 1
下面的代碼將 "books.xml" 加載到 xmlDoc 中,并刪除第一個 <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 y = xmlDoc.getElementsByTagName("book")[0]; var x = xmlDoc.documentElement.removeChild(y); document.getElementById("demo").innerHTML = "Removed node: " + x.nodeName; }
例子 2
從節點列表中刪除最后一個子節點:
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 len = xmlDoc.getElementsByTagName('book').length; var y = xmlDoc.getElementsByTagName("book")[len-1]; var x = xmlDoc.documentElement.removeChild(y); document.getElementById("demo").innerHTML = "Removed node: " + x.nodeName; }