📄 dom_nodes_replace.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 - Replace 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 Replace Nodes</h1>
<a href="dom_nodes_remove.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_create.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />
<p class="intro">The replaceChild() method replaces a specified node.</p>
<p class="intro">The nodeValue property replaces text in a text node.</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_replacechild">Replace an
element node</a><br />
This example uses replaceChild() to replace the first <book> node.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_replacedata2">Replace data in a text node</a><br />
This example uses the nodeValue property to replace data in a text
node.<br />
</p>
<hr />
<h2>Replace an Element Node</h2>
<p>The replaceChild() method is used to replace a node.</p>
<p>The following code fragment replaces the first
<book> element:</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table21">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement;
//create a book element, title element and a text node
newNode=xmlDoc.createElement("book");
newTitle=xmlDoc.createElement("title");
newText=xmlDoc.createTextNode("A Notebook");
//add the text node to the title node,
newTitle.appendChild(newText);
//add the title node to the book node
newNode.appendChild(newTitle);
y=xmlDoc.getElementsByTagName("book")[0]
//replace the first book node with the new node
x.replaceChild(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>Create a new element node <title></li>
<li>Create a new text node with the text "A Notebook"</li>
<li>Append the new text node to the new element node <title></li>
<li>Append the new element node <title> to the new element node <book></li>
<li>Replace the first <book> element node with the new <book> element node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_replacechild">Try it
yourself</a></p>
<hr />
<h2>Replace Data In a Text Node</h2>
<p>The replaceData() method is used to replace data in a
text node.</p>
<p>The replaceData() method has three parameters:</p>
<ul>
<li>offset - Where to begin replacing characters. Offset value starts at zero</li>
<li>length - How many characters to replace</li>
<li>string - The string to insert</li>
</ul>
<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.replaceData(0,8,"Easy");</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>Get the text node of the first <title> element node</li>
<li>Use the replaceDat method to replace the eight first characters from the
text node with "Easy"</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_replacedata">Try it
yourself</a></p>
<hr />
<h2>Use the nodeValue Property Instead</h2>
<p>It is easier to replace the data in a text node using the
nodeValue property.</p>
<p>The following code fragment will replace the text
node value in the first
<title>
element with "Easy Italian":</p>
<table width="100%" border="1" class="ex" cellspacing="0" id="table23">
<tr><td>
<pre>xmlDoc=loadXMLDoc("books.xml");</pre>
<pre>x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];</pre>
<pre>x.nodeValue="Easy Italian";</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>Get the text node of the first <title> element node</li>
<li>Use the nodeValue property to change the text of the text node</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_replacedata2">Try it
yourself</a></p>
<p>You can read more about changing node values in the <a href="dom_nodes_set.asp">Change Node chapter</a>.</p>
<hr />
<a href="dom_nodes_remove.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_create.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 + -