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

📄 dom_nodes_set.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 - Change Node Values</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 Change Node Values</h1>

<a href="dom_nodes_get.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_remove.asp"><img border="0" src="../images/btn_next.gif" alt="next" width="100" height="20" /></a>
<hr />

<p class="intro">The nodeValue property is used to change a node value.</p>
<p class="intro">The setAttribute() method is used to change an attribute value.</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_change_nodevalue">Change 
an elements text node</a><br />
This example uses the nodeValue property to change the text node of the first &lt;title&gt; 
element in &quot;books.xml&quot;.</p>
<p><a target="_blank" href="tryit.asp@filename=try_dom_setattribute1">Change an 
attributes value using setAttribute</a><br />
This example uses the setAttribute() method to change the value of the &quot;category&quot; 
attribute of the first &lt;book&gt;.</p>
<p><a href="tryit.asp@filename=try_dom_att_nodevalue">Change an attributes value 
using nodeValue</a><br />
This example use the nodeValue property to change the value of the &quot;category&quot; 
attribute of the first &lt;book&gt;.</p>
<hr />

<h2>Change the Value of an Element</h2>
<p>In the DOM, everything is a node. Element nodes does not have a text value.</p>
<p>The text of an element node is stored in a child node. This node is called a 
text node.</p>
<p>The way to change the text of an element, is to change the value of the child 
node (text node).</p>
<hr />

<h2>Change the Value of a Text Node</h2>
<p>The nodeValue property can be used to change the value of a text node. </p>
<p>The following code changes the text node value of the first &lt;title&gt; element:</p>
<table class="ex" id="table27" border="1" cellspacing="0" width="100%">
	<tr>
		<td>
		<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
		<pre>x=xmlDoc.getElementsByTagName(&quot;title&quot;)[0].childNodes[0];
x.nodeValue=&quot;Easy Cooking&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</li>
	<li>Change the node value of the text node to &quot;Easy Cooking&quot;</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_change_nodevalue">Try 
it yourself</a></p>
<p>Loop through and change the text node of all &lt;title&gt; elements:
<a target="_blank" href="tryit.asp@filename=try_dom_change_nodevalue2">Try 
it yourself</a><br />
</p>
<hr />

<h2>Change the Value of an Attribute</h2>
<p>In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have 
text values.</p>
<p>The way to change the value of an attribute, is to change its text value.</p>
<p>This can be done using the setAttribute() method or using the nodeValue 
property of the attribute node.</p>
<hr />
<h2>Change an Attribute Using setAttribute()</h2>
<p>The setAttribute() method changes the value of an existing 
attribute, or creates a new attribute.</p>
<p>The following code changes the category attribute of the &lt;book&gt; element:</p>
<table class="ex" id="table28" border="1" cellspacing="0" width="100%">
	<tr>
		<td>
		<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
		<pre>x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute(&quot;category&quot;,&quot;food&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 first &lt;book&gt; element</li>
	<li>Change the &quot;category&quot; attribute value to &quot;food&quot;</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_setattribute1">Try 
it yourself</a></p>
<p>Loop through all &lt;title&gt; elements and add a new attribute:
<a target="_blank" href="tryit.asp@filename=try_dom_setattribute2">Try 
it yourself</a></p>
<p><b>Note:</b> If the attribute does not exist, a new attribute is created 
(with the name and value specified).<br />
</p>
<hr />

<h2>Change an Attribute Using nodeValue</h2>
<p>The nodeValue property can be used to change the value of a attribute node:</p>

<table width="100%" border="1" class="ex" cellspacing="0" id="table26">
<tr><td>
<pre>xmlDoc=loadXMLDoc(&quot;books.xml&quot;);</pre>
<pre>x=xmlDoc.getElementsByTagName(&quot;book&quot;)[0]
y=x.getAttributeNode(&quot;category&quot;);
y.nodeValue=&quot;food&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 &quot;category&quot; attribute of the first &lt;book&gt; element</li>
	<li>Change the attribute node value to &quot;food&quot;</li>
</ol>
<p><a target="_blank" href="tryit.asp@filename=try_dom_att_nodevalue">Try it yourself</a></p>
<hr />
<a href="dom_nodes_get.asp"><img border="0" src="../images/btn_previous.gif" alt="prev" width="100" height="20" /></a>
<a href="dom_nodes_remove.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 + -