# XML DOM 刪除節點
removeChild() 方法刪除指定節點。
removeAttribute() 方法刪除指定屬性。
## 嘗試一下 - 實例
下面的實例使用 XML 文件 [books.xml](images/books.xml)。
函數 [loadXMLDoc()](dom-loadxmldoc.html),位于外部 JavaScript 中,用于加載 XML 文件。
[刪除元素節點](/try/try.php?filename=try_dom_removechild)
本例使用 removeChild() 來刪除第一個 <book> 元素。
```
<!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>
```
[刪除當前元素節點](/try/try.php?filename=try_dom_removecurrent)
本例使用 parentNode 和 removeChild() 來刪除當前的 <book> 元素。
```
<!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>
```
[刪除文本節點](/try/try.php?filename=try_dom_removetextnode)
本例使用 removeChild() 來刪除第一個 <title> 元素的文本節點。
```
<!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>
```
[清空文本節點的文本](/try/try.php?filename=try_dom_remove_nodevalue)
本例使用 nodeValue() 屬性來清空第一個 <title> 元素的文本節點。
```
<!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>
```
[根據名稱刪除屬性](/try/try.php?filename=try_dom_removeattribute)
本例使用 removeAttribute() 從第一個 <book> 元素中刪除 "category" 屬性。
```
<!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>
```
[根據對象刪除屬性](/try/try.php?filename=try_dom_removeattributenode)
本例使用 removeAttributeNode() 從所有 <book> 元素中刪除所有屬性。
```
<!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>
```
## 刪除元素節點
removeChild() 方法刪除指定的節點。
當一個節點被刪除時,其所有子節點也會被刪除。
下面的代碼片段將從載入的 xml 中刪除第一個 <book> 元素:
## 實例
```
xmlDoc=loadXMLDoc("books.xml");
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
```
實例解釋:
1. 使用 [loadXMLDoc()](dom-loadxmldoc.html) 把 "[books.xml](images/books.xml)" 載入 xmlDoc 中
2. 把變量 y 設置為要刪除的元素節點
3. 通過使用 removeChild() 方法從父節點刪除元素節點
## 刪除自身 - 刪除當前的節點
removeChild() 方法是唯一可以刪除指定節點的方法。
當您已導航到需要刪除的節點時,就可以通過使用 parentNode 屬性和 removeChild() 方法來刪除此節點:
## 實例
```
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
```
實例解釋:
1. 使用 [loadXMLDoc()](dom-loadxmldoc.html) 把 "[books.xml](images/books.xml)" 載入 xmlDoc 中
2. 把變量 y 設置為要刪除的元素節點
3. 通過使用 parentNode 屬性和 removeChild() 方法來刪除此元素節點
## 刪除文本節點
removeChild() 方法可用于刪除文本節點:
## 實例
```
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
```
實例解釋:
1. 使用 [loadXMLDoc()](dom-loadxmldoc.html) 把 "[books.xml](images/books.xml)" 載入 xmlDoc 中
2. 把變量 x 設置為第一個 title 元素節點
3. 把變量 y 設置為要刪除的文本節點
4. 通過使用 removeChild() 方法從父節點刪除元素節點
不太常用 removeChild() 從節點刪除文本。可以使用 nodeValue 屬性代替它。請看下一段。
## 清空文本節點
nodeValue 屬性可用于改變或清空文本節點的值:
## 實例
```
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";
```
實例解釋:
1. 使用 [loadXMLDoc()](dom-loadxmldoc.html) 把 "[books.xml](images/books.xml)" 載入 xmlDoc 中
2. 把變量 x 設置為第一個 title 元素的文本節點
3. 使用 nodeValue 屬性來清空文本節點的文本
遍歷并更改所有 <title> 元素的文本節點: [嘗試一下](/try/try.php?filename=try_dom_remove_nodevalue2)
## 根據名稱刪除屬性節點
removeAttribute(_name_) 方法用于根據名稱刪除屬性節點。
實例:removeAttribute('category')
下面的代碼片段刪除第一個 <book> 元素中的 "category" 屬性:
## 實例
```
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
```
實例解釋:
1. 使用 [loadXMLDoc()](dom-loadxmldoc.html) 把 "[books.xml](images/books.xml)" 載入 xmlDoc 中
2. 使用 getElementsByTagName() 來獲取 book 節點
3. 從第一個 book 元素節點中刪除 "category" 屬性
遍歷并刪除所有 <book> 元素的 "category" 屬性: [嘗試一下](/try/try.php?filename=try_dom_removeattribute2)
## 根據對象刪除屬性節點
removeAttributeNode(_node_) 方法通過使用 node 對象作為參數,來刪除屬性節點。
實例: removeAttributeNode(x)
下面的代碼片段刪除所有 <book> 元素的所有屬性:
## 實例
```
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);
}
}
```
實例解釋:
1. 使用 [loadXMLDoc()](dom-loadxmldoc.html) 把 "[books.xml](images/books.xml)" 載入 xmlDoc 中
2. 使用 getElementsByTagName() 來獲取所有 book 節點
3. 檢查每個 book 元素是否擁有屬性
4. 如果在某個 book 元素中存在屬性,則刪除該屬性
- XML 基礎
- XML 簡介
- XML 用途
- XML 樹結構
- XML 語法規則
- XML 元素
- XML 屬性
- XML 驗證
- 查看 XML 文件
- 使用 CSS 顯示 XML
- 使用 XSLT 顯示 XML
- XML Javascript
- XMLHttpRequest 對象
- XML Parser
- XML DOM
- XML to HTML
- XML 應用程序
- XML 進階
- XML 命名空間
- XML CDATA
- XML 編碼
- 服務器上的 XML
- XML DOM 高級
- XML 注意事項
- XML 相關技術
- 現實生活中的 XML
- XML 編輯器
- XML - E4X
- DTD 教程
- DTD 簡介
- DTD - XML 構建模塊
- DTD - 元素
- DTD - 屬性
- XML 元素 vs. 屬性
- DTD - 實體
- DTD 驗證
- DTD - 來自網絡的實例
- XML DOM
- XML DOM 簡介
- XML DOM 節點
- XML DOM 節點樹
- XML DOM 解析器
- XML DOM 加載函數
- XML DOM - 屬性和方法
- XML DOM - 訪問節點
- XML DOM 節點信息
- XML DOM 節點列表
- XML DOM 遍歷節點樹
- XML DOM 瀏覽器差異
- XML DOM - 導航節點
- XML DOM 獲取節點值
- XML DOM 改變節點值
- XML DOM 刪除節點
- XML DOM 替換節點
- XML DOM 創建節點
- XML DOM 添加節點
- XML DOM 克隆節點
- The XMLHttpRequest 對象
- XML DOM 節點類型
- XML DOM - Node 對象
- XML DOM - NodeList 對象
- XML DOM - NamedNodeMap 對象
- XML DOM - Document 對象
- XML DOM - DocumentImplementation 對象
- XML DOM - DocumentType 對象
- XML DOM - ProcessingInstruction 對象
- XML DOM - Element 對象
- XML DOM - Attr 對象
- XML DOM - Text 對象
- XML DOM - CDATASection 對象
- XML DOM - Comment 對象
- XMLHttpRequest 對象
- XML DOM Parse Error 對象
- XML DOM 解析器錯誤
- XSLT 教程
- XSL 語言
- XSLT 簡介
- XSLT 瀏覽器
- XSLT - 轉換
- XSLT <xsl:template> 元素
- XSLT <xsl:value-of> 元素
- XSLT <xsl:for-each> 元素
- XSLT <xsl:sort> 元素
- XSLT <xsl:if> 元素
- XSLT <xsl:choose> 元素
- XSLT <xsl:apply-templates> 元素
- XSLT - 在客戶端
- XSLT - 在服務器端
- XSLT - 編輯 XML
- XML 編輯器
- XSLT 元素參考手冊
- XSLT 函數
- XPath 教程
- XPath 簡介
- XPath 節點
- XPath 語法
- XPath 軸(Axes)
- XPath 運算符
- XPath Examples
- XPath、XQuery 以及 XSLT 函數函數參考手冊
- 函數參考手冊
- XQuery 教程
- XQuery 簡介
- XQuery 實例
- XQuery FLWOR 表達式
- XQuery FLWOR + HTML
- XQuery 術語
- XQuery 語法
- XQuery 添加元素 和屬性
- XQuery 選擇 和 過濾
- XQuery 函數
- XQuery 參考手冊
- XLink 和 XPointer 教程
- XLink 和 XPointer 簡介
- XLink 和 XPointer 語法
- XLink 實例
- XPointer 實例
- XLink 參考手冊
- XML Schema 教程
- XML Schema 簡介
- 為什么使用 XML Schemas?
- XSD 如何使用?
- XSD - <schema> 元素
- XSD 簡易元素
- XSD 屬性
- XSD 限定 / Facets
- XSD 復合元素
- XSD 空元素
- XSD 僅含元素
- XSD 僅含文本
- XSD 混合內容
- XSD 指示器
- XSD <any> 元素
- XSD <anyAttribute> 元素
- XSD 元素替換(Element Substitution)
- XSD 實例
- XSD 字符串 數據類型
- XSD 日期和時間數據類型
- XSD 數值數據類型
- XSD 雜項 數據類型
- XML 編輯器
- XML Schema 參考手冊
- XSD 元素
- XSD 限定/Facets
- SOAP 教程
- SOAP 簡介
- SOAP 語法
- SOAP Envelope 元素
- SOAP Header 元素
- SOAP Body 元素
- SOAP Fault 元素
- SOAP HTTP 協議
- SOAP 實例
- WSDL 教程
- WSDL 簡介
- WSDL 文檔
- WSDL 端口
- WSDL 綁定
- WSDL UDDI
- RSS 教程
- RSS 簡介
- RSS 歷史
- RSS 語法
- RSS <channel> 元素
- RSS <item> 元素
- RSS 發布您的 Feed
- RSS 閱讀器
- RSS 參考手冊
- RDF 教程
- RDF 簡介
- RDF 規則
- RDF 實例
- RDF 主要 元素
- RDF 容器 Elements
- RDF 集合
- RDF Schema (RDFS)
- RDF 都柏林核心元數據倡議
- OWL 簡介
- RDF 參考手冊
- XSL-FO 教程
- XSL-FO 簡介
- XSL-FO 文檔
- XSL-FO 區域
- XSL-FO 輸出
- XSL-FO 流
- XSL-FO 頁面
- XSL-FO 塊
- XSL-FO 列表
- XSL-FO 表格
- XSL-FO 與 XSLT
- XSL-FO 軟件
- XSL-FO 參考手冊
- SVG 教程
- SVG 簡介
- SVG 實例
- SVG 在 HTML 頁面
- SVG <rect>
- SVG <circle>
- SVG <ellipse>
- SVG <line>
- SVG <polygon>
- SVG <polyline>
- SVG <path>
- SVG <text>
- SVG Stroke 屬性
- SVG 濾鏡
- SVG 模糊效果
- SVG 陰影
- SVG 漸變 - 線性
- SVG 漸變- 放射性
- SVG 參考手冊
- 免責聲明