📄 dom_nodes_remove.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>XML DOM - Remove Nodes</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>XML DOM Remove Nodes</h1>
<a href="dom_nodes_set.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_replace.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />
<p class="intro">The removeChild() method removes a specified node.</p>
<p class="intro">The removeAttribute() method removes a specified attribute.</p>
<hr />
<h2>Examples
</h2>
<p>The examples below use the XML file
<a target="_blank" href="books.xml">books.xml</a>. <br />
A function, <a href="dom_loadxmldoc.asp">loadXMLDoc()</a>, in an external JavaScript is used to load the XML file.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removechild">Remove an
element node</a><br />
This example uses removeChild() to remove the first <book>
element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removecurrent">Remove the
current element node</a><br />
This example uses parentNode and removeChild() to remove the current <book>
element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removetextnode">Remove a
text node</a><br />
This example uses removeChild() to remove the text node from the first <title>
element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_remove_nodevalue">Clear
the text of a text node</a><br />
This example uses the nodeValue() property to clear the text node of the first
<title> element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removeattribute">Remove
an attribute by name</a><br />
This example uses removeAttribute() to remove the "category"
attribute from the first <book> element.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removeattributenode">
Remove attributes by object</a><br />
This example uses removeAttributeNode() to remove all
attributes from all <book> elements.</p>
<hr />
<h2>Remove an Element Node</h2>
<p>The removeChild() method removes a specified node.</p>
<p>When a node is removed, all its child nodes are also removed.</p>
<p>The following code fragment will remove the first <book> element from the loaded xml:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table18">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>y=xmlDoc.getElementsByTagName("book")[0];</pre>
<pre>xmlDoc.documentElement.removeChild(y);</pre>
</td></tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
<li>Set the variable y to be the element node to remove</li>
<li>Remove the element node by using the removeChild() method from the
parent node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removechild">Try it
yourself</a></p>
<hr />
<h2>Remove Myself - Remove the Current Node</h2>
<p>The removeChild() method is the only way to removes a specified node.</p>
<p>When you have navigated to the node you want to remove, it is possible to
remove that node using the parentNode property and the removeChild() method:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table44">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("book")[0];</pre>
<pre>x.parentNode.removeChild(x);</pre>
</td></tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
<li>Set the variable y to be the element node to remove</li>
<li>Remove the element node by using the parentNode property and the
removeChild() method</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removecurrent">Try it
yourself</a></p>
<hr />
<h2>Remove a Text Node</h2>
<p>The removeChild() method can also be used to remove a text node:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table43">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);</pre>
</td></tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
<li>Set the variable x to be the first title element node</li>
<li>Set the variable y to be the text node to remove</li>
<li>Remove the element node by using the removeChild() method from the
parent node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removetextnode">Try it
yourself</a></p>
<p>It is not very common to use removeChild() just to remove the text from a
node. The nodeValue property can be used instead. See next paragraph.</p>
<hr />
<h2>Clear a Text Node</h2>
<p>The nodeValue property can be used to change or clear the value of a text
node:</p>
<table class="ex" id="table42" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";</pre>
</td>
</tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
<li>Set the variable x to be the text node of the first title element</li>
<li>Use the nodeValue property to clear the text from the text node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_remove_nodevalue">Try
it yourself</a></p>
<p>Loop through and change the text node of all <title> elements:
<a target="_blank" href="tryit.asp@filename=try_dom_remove_nodevalue2">Try
it yourself</a><br />
</p>
<hr />
<h2>Remove an Attribute Node by Name</h2>
<p>The removeAttribute(<i>name</i>) method is used to remove an attribute node
by its name.</p>
<p>Example: removeAttribute('category')</p>
<p>The following code fragment removes the "category" attribute
in the first <book> element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table17">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");</pre>
</td></tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
<li>Use getElementsByTagName() to get book nodes</li>
<li>Remove the "category" attribute form the first book element node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removeattribute">Try
it yourself</a></p>
<p>Loop through and remove the "category" attribute of all <book> elements:
<a target="_blank" href="tryit.asp@filename=try_dom_removeattribute2">Try
it yourself</a></p>
<hr />
<h2>Remove Attribute Nodes by Object</h2>
<p>The removeAttributeNode(<i>node</i>) method is used to remove an attribute node,
using the node object as parameter.</p>
<p>Example: removeAttributeNode(x)</p>
<p>The following code fragment removes all the attributes of all <book> elements:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table29">
<tr><td>
<pre>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);
}
}</pre>
</td></tr>
</table>
<p>Example explained:</p>
<ol>
<li>Load "<a target="_blank" href="books.xml">books.xml</a>"
into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
<li>Use getElementsByTagName() to get all book nodes</li>
<li>For each book element check if there are any attributes</li>
<li>While there are attributes in a book element, remove the attribute</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_removeattributenode">Try
it yourself</a></p>
<hr />
<a href="dom_nodes_set.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_replace.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -