📄 dom_nodes_add.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 - Add 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 Add Nodes</h1>
<a href="dom_nodes_create.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_clone.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<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_createelement2">Add a
node after the last child node</a><br />
This example uses appendChild() to
add a child node to an existing node.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_insertbefore">Add a node before a specified
child node</a><br />
This example uses insertBefore() to insert a node before a specified child node.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createattribute3">Add a new attribute</a><br />
This example uses the setAttribute() method to add a new attribute.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_insertdata">Add data to a text node</a><br />
This example uses insertData() to insert data into an existing text node.<br />
</p>
<hr />
<h2>Add a Node - appendChild()</h2>
<p>The appendChild() method adds a child node to an existing node. </p>
<p>The new node is added (appended) after any existing child nodes.</p>
<p><b>Note:</b> Use insertBefore() if the position of the node is important.</p>
<p>The following code fragment creates an element (<edition>),
and adds it after the last child of the first
<book> element:</p>
<table class="ex" id="table25" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);</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>Create a new node <edition></li>
<li>Append the node to the first <book> element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createelement2">Try
it yourself</a></p>
<p>Loop through and append an element to all <book> elements:
<a target="_blank" href="tryit.asp@filename=try_dom_createelement">Try
it yourself</a></p>
<hr />
<h2>Insert a Node - insertBefore()</h2>
<p>The insertBefore() method is used to insert a node before a specified child node.</p>
<p>This method is useful when the position of the added node is important:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table23">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>newNode=xmlDoc.createElement("book");</pre>
<pre>x=xmlDoc.documentElement;
y=xmlDoc.getElementsByTagName("book")[3];</pre>
<pre>x.insertBefore(newNode,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>Create a new element node <book></li>
<li>Insert the new node in front of the last <book> element node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_insertbefore">Try it
yourself</a></p>
<p>If the second parameter of insertBefore() is null, the new node will be added
after the last existing child node.</p>
<p><b>x.insertBefore(newNode,null)</b> and <b>x.appendChild(newNode)</b> will
both append a new child node to x.</p>
<hr />
<h2>Add a New Attribute</h2>
<p>There is no method called addAtribute().</p>
<p>The setAttribute() method creates a new attribute if the attribute does
not exist:</p>
<table class="ex" id="table26" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");</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 (create) the attribute "edition" with the value "first"
for the first <book> element</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_createattribute3">Try
it yourself</a></p>
<p><b>Note:</b> If the attribute already exists, the setAttribute()
method will overwrite the existing value.</p>
<hr />
<h2>Add Text to a Text Node - insertData()</h2>
<p>The insertData() method inserts data into an existing
text node.</p>
<p>The insertData() method has two parameters:</p>
<ul>
<li>offset - Where to begin inserting characters (starts at zero)</li>
<li>string - The string to insert</li>
</ul>
<p>The following code fragment will add "Easy" to the text
node of the first
<title>
element of the loaded XML:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table22">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];</pre>
<pre>x.insertData(0,"Easy ");</pre>
</td></tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=try_dom_insertdata">Try it yourself</a></p>
<hr />
<a href="dom_nodes_create.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_clone.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 + -