XML DOM 刪除節(jié)點(diǎn)
removeChild() 方法刪除指定節(jié)點(diǎn)。
removeAttribute() 方法刪除指定屬性。
嘗試一下 - 實(shí)例
下面的實(shí)例使用 XML 文件 books.xml。
函數(shù) loadXMLDoc(),位于外部 JavaScript 中,用于加載 XML 文件。
刪除元素節(jié)點(diǎn)
本例使用 removeChild() 來(lái)刪除第一個(gè) <book> 元素。
刪除當(dāng)前元素節(jié)點(diǎn)
本例使用 parentNode 和 removeChild() 來(lái)刪除當(dāng)前的 <book> 元素。
刪除文本節(jié)點(diǎn)
本例使用 removeChild() 來(lái)刪除第一個(gè) <title> 元素的文本節(jié)點(diǎn)。
清空文本節(jié)點(diǎn)的文本
本例使用 nodeValue() 屬性來(lái)清空第一個(gè) <title> 元素的文本節(jié)點(diǎn)。
根據(jù)名稱刪除屬性
本例使用 removeAttribute() 從第一個(gè) <book> 元素中刪除 "category" 屬性。
根據(jù)對(duì)象刪除屬性
本例使用 removeAttributeNode() 從所有 <book> 元素中刪除所有屬性。
刪除元素節(jié)點(diǎn)
removeChild() 方法刪除指定的節(jié)點(diǎn)。
當(dāng)一個(gè)節(jié)點(diǎn)被刪除時(shí),其所有子節(jié)點(diǎn)也會(huì)被刪除。
下面的代碼片段將從載入的 xml 中刪除第一個(gè) <book> 元素:
實(shí)例
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"> </script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); document.write("Number of book nodes: "); document.write(xmlDoc.getElementsByTagName('book').length); document.write("<br>"); y=xmlDoc.getElementsByTagName("book")[0]; xmlDoc.documentElement.removeChild(y); document.write("Number of book nodes after removeChild(): "); document.write(xmlDoc.getElementsByTagName('book').length); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例解釋:
使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
把變量 y 設(shè)置為要?jiǎng)h除的元素節(jié)點(diǎn)
通過使用 removeChild() 方法從父節(jié)點(diǎn)刪除元素節(jié)點(diǎn)
刪除自身 - 刪除當(dāng)前的節(jié)點(diǎn)
removeChild() 方法是唯一可以刪除指定節(jié)點(diǎn)的方法。
當(dāng)您已導(dǎo)航到需要?jiǎng)h除的節(jié)點(diǎn)時(shí),就可以通過使用 parentNode 屬性和 removeChild() 方法來(lái)刪除此節(jié)點(diǎn):
實(shí)例
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"> </script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); document.write("Number of book nodes before removeChild(): "); document.write(xmlDoc.getElementsByTagName("book").length); document.write("<br>"); x=xmlDoc.getElementsByTagName("book")[0] x.parentNode.removeChild(x); document.write("Number of book nodes after removeChild(): "); document.write(xmlDoc.getElementsByTagName("book").length); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例解釋:
使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
把變量 y 設(shè)置為要?jiǎng)h除的元素節(jié)點(diǎn)
通過使用 parentNode 屬性和 removeChild() 方法來(lái)刪除此元素節(jié)點(diǎn)
刪除文本節(jié)點(diǎn)
removeChild() 方法可用于刪除文本節(jié)點(diǎn):
實(shí)例
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"> </script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; document.write("Child nodes: "); document.write(x.childNodes.length); document.write("<br>"); y=x.childNodes[0]; x.removeChild(y); document.write("Child nodes: "); document.write(x.childNodes.length); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例解釋:
使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
把變量 x 設(shè)置為第一個(gè) title 元素節(jié)點(diǎn)
把變量 y 設(shè)置為要?jiǎng)h除的文本節(jié)點(diǎn)
通過使用 removeChild() 方法從父節(jié)點(diǎn)刪除元素節(jié)點(diǎn)
不太常用 removeChild() 從節(jié)點(diǎn)刪除文本。可以使用 nodeValue 屬性代替它。請(qǐng)看下一段。
清空文本節(jié)點(diǎn)
nodeValue 屬性可用于改變或清空文本節(jié)點(diǎn)的值:
實(shí)例
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"> </script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; document.write("Value: " + x.nodeValue); document.write("<br>"); x.nodeValue=""; document.write("Value: " + x.nodeValue); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例解釋:
使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
把變量 x 設(shè)置為第一個(gè) title 元素的文本節(jié)點(diǎn)
使用 nodeValue 屬性來(lái)清空文本節(jié)點(diǎn)的文本
遍歷并更改所有 <title> 元素的文本節(jié)點(diǎn):嘗試一下
根據(jù)名稱刪除屬性節(jié)點(diǎn)
removeAttribute(name) 方法用于根據(jù)名稱刪除屬性節(jié)點(diǎn)。
實(shí)例:removeAttribute('category')
下面的代碼片段刪除第一個(gè) <book> 元素中的 "category" 屬性:
實(shí)例
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"> </script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); document.write(x[0].getAttribute('category')); document.write("<br>"); x[0].removeAttribute('category'); document.write(x[0].getAttribute('category')); </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例解釋:
使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
使用 getElementsByTagName() 來(lái)獲取 book 節(jié)點(diǎn)
從第一個(gè) book 元素節(jié)點(diǎn)中刪除 "category" 屬性
遍歷并刪除所有 <book> 元素的 "category" 屬性:嘗試一下
根據(jù)對(duì)象刪除屬性節(jié)點(diǎn)
removeAttributeNode(node) 方法通過使用 node 對(duì)象作為參數(shù),來(lái)刪除屬性節(jié)點(diǎn)。
實(shí)例: removeAttributeNode(x)
下面的代碼片段刪除所有 <book> 元素的所有屬性:
實(shí)例
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"> </script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); for (i=0;i<x.length;i++) { while (x[i].attributes.length>0) { attnode=x[i].attributes[0]; old_att=x[i].removeAttributeNode(attnode); document.write("Removed: " + old_att.nodeName) document.write(": " + old_att.nodeValue) document.write("<br>") } } </script> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
實(shí)例解釋:
使用 loadXMLDoc() 把 "books.xml" 載入 xmlDoc 中
使用 getElementsByTagName() 來(lái)獲取所有 book 節(jié)點(diǎn)
檢查每個(gè) book 元素是否擁有屬性
如果在某個(gè) book 元素中存在屬性,則刪除該屬性