📄 dom_nodes_access.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>HTML DOM Access 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>HTML DOM Access Nodes</h1>
<a href="dom_methods.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" /></a>
<a href="dom_nodes_info.asp"><img border="0" alt="next" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">With the DOM, you can access every node in an HTML document.</p>
<hr />
<h2>Accessing Nodes</h2>
<p>You can access a node in three ways:</p>
<p>1. By using the getElementById() method</p>
<p>2. By using the getElementsByTagName() method</p>
<p>3. By navigating the node tree, using the node relationships.</p>
<hr />
<h2>The getElementById() Method</h2>
<p>The getElementById() method returns the element with the specified ID:</p>
<h3>Syntax</h3>
<table border="1" width="100%" class="ex" cellspacing="0" id="table13">
<tr>
<td valign="top">
<pre>document.getElementById("<i>someID</i>");</pre>
</td>
</tr>
</table>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_getelementbyid">Try it
yourself.</a></p>
<p><b>Note:</b> The getElementById() method doesn't work in XML.</p>
<hr />
<h2>The getElementsByTagName() Method</h2>
<p>getElementsByTagName() returns all elements with a specified tag name.</p>
<h3>Syntax</h3>
<table class="ex" id="table18" border="1" cellspacing="0" width="100%">
<tr>
<td valign="top">
<pre><i>node</i>.getElementsByTagName(<i>"tagname"</i>);</pre>
</td>
</tr>
</table>
<h3>Example 1</h3>
<p>The following example returns a nodeList of all <p> elements in the document:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table23">
<tr>
<td valign="top">
<pre>document.getElementsByTagName("p");</pre>
</td>
</tr>
</table>
<p>
<a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_getelementsbytagname">Try it
yourself.</a></p>
<h3>Example 2</h3>
<p>The following example returns a nodeList of all <p> elements that are descendants of
the element with id="main":</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table24">
<tr>
<td valign="top">
<pre>document.getElementById('main').getElementsByTagName("p");</pre>
</td>
</tr>
</table>
<p>
<a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_getelementsbytagname2">Try it
yourself.</a></p>
<hr />
<h2>DOM Node List</h2>
<p>The getElementsByTagName() method returns a node list. A node list is an
array of nodes.</p>
<p>The following code stores a list of <p> nodes (a node list) in the variable
x:</p>
<table class="ex" id="table21" border="1" cellspacing="0" width="100%">
<tr>
<td valign="top">
<pre>x=document.getElementsByTagName("p");</pre>
</td>
</tr>
</table>
<p>The <p> elements in x can be accessed by index number. To access the
second
<p> you can write:</p>
<table class="ex" id="table22" border="1" cellspacing="0" width="100%">
<tr>
<td valign="top">
<pre>y=x[1];</pre>
</td>
</tr>
</table>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_nodelist">Try it
yourself.</a></p>
<p><b>Note:</b> The index starts at 0.</p>
<p>You will learn more about node lists in a later chapter of this tutorial.</p>
<hr />
<h2>DOM Node List Length</h2>
<p>The length property defines the length of a node list (the number of nodes).</p>
<p>You can loop through a node list by using the length property:</p>
<table class="ex" id="table25" border="1" cellspacing="0" width="100%">
<tr>
<td valign="top">
<pre>x=document.getElementsByTagName("p");</pre>
<pre>for (i=0;i<x.length;i++)
{
document.write(x[i].innerHTML);
document.write("<br />");
}</pre>
</td>
</tr>
</table>
<p>Example explained:</p>
<ol>
<li>Get all <p> element nodes</li>
<li>For each <p> element, output the value of its text node</li>
</ol>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_nodelist_length">
Try it yourself.</a></p>
<hr />
<h2>Navigating Node Relationships</h2>
<p>The three properties parentNode, firstChild,
and lastChild follow the document structure and allow short-distance travel in
the document.</p>
<p>Look at the following HTML fragment:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table9">
<tr>
<td valign="top">
<pre><html>
<body>
<p id="intro">W3Schools example</p>
<div id="main">
<p id="main1">The DOM is very useful</p>
<p id="main2">This example demonstrates <b>node relationships</b></p>
</div>
</body>
</html></pre>
</td>
</tr>
</table>
<p>In the HTML code above, the <p id="intro"> is the first child node (firstChild) of the <body>
element, and the <div> element is the last child node (lastChild) of the <body> element.</p>
<p>Furthermore, the <body> is the parent (parentNode) of the every <p> element.</p>
<p>The firstChild property can be used to access the text of an element:</p>
<table border="1" width="100%" class="ex" cellspacing="0" id="table11">
<tr>
<td valign="top">
<pre>var x=document.getElementById("intro");<br />var text=x.firstChild.nodeValue;</pre>
</td>
</tr>
</table>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_firstchild">Try
it yourself.</a></p>
<hr />
<h2>Root Nodes</h2>
<p>There are two special document properties that allow access to the
tags:</p>
<ul>
<li>document.documentElement</li>
<li>document.body</li>
</ul>
<p>The first property returns the root node of the document and exists in
all XML and HTML documents.</p>
<p>The second property is a special addition for HTML pages, and gives
direct access to the <body> tag.</p>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_root">Try
it yourself.</a></p>
<hr />
<a href="dom_methods.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="dom_nodes_info.asp"><img border="0" alt="next" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -