⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dom_nodes_replace.asp@output=print

📁 W3Schools tutorial..web designing
💻 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 &lt;book&gt; 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 
&lt;book&gt; element:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table21">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);

x=xmlDoc.documentElement;

//create a book element, title element and a text node
newNode=xmlDoc.createElement(&quot;book&quot;);
newTitle=xmlDoc.createElement(&quot;title&quot;);
newText=xmlDoc.createTextNode(&quot;A Notebook&quot;);

//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(&quot;book&quot;)[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 &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
	<li>Create a new element node &lt;book&gt;</li>
	<li>Create a new element node &lt;title&gt;</li>
	<li>Create a new text node with the text &quot;A Notebook&quot;</li>
	<li>Append the new text node to the new element node &lt;title&gt;</li>
	<li>Append the new element node &lt;title&gt; to the new element node &lt;book&gt;</li>
	<li>Replace the first &lt;book&gt; element node with the new &lt;book&gt; 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(&quot;books.xml&quot;);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;)[0].childNodes[0];</pre>
<pre>x.replaceData(0,8,&quot;Easy&quot;);</pre>
</td></tr>
</table>

<p>Example explained:</p>
<ol>
	<li>Load &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
	<li>Get the text node of the first &lt;title&gt; element node</li>
	<li>Use the replaceDat method to replace the eight first characters from the 
	text node with &quot;Easy&quot;</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 
&lt;title&gt; 
element with &quot;Easy Italian&quot;:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table23">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;)[0].childNodes[0];</pre>
<pre>x.nodeValue=&quot;Easy Italian&quot;;</pre>
</td></tr>
</table>

<p>Example explained:</p>
<ol>
	<li>Load &quot;<a target="_blank" href="books.xml">books.xml</a>&quot; 
	into xmlDoc using <a href="dom_loadxmldoc.asp">loadXMLDoc()</a> </li>
	<li>Get the text node of the first &lt;title&gt; 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 + -