📄 dom_methods.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 Properties and Methods</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 Properties and Methods</h1>
<a href="dom_nodetree.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" /></a>
<a href="dom_nodes_access.asp"><img border="0" alt="next" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">Properties and methods define the programming interface to the
HTML DOM.</p>
<hr />
<h2>Programming Interface</h2>
<p>The DOM models HTML as a set of node objects. The nodes can be accessed with
JavaScript or other programming languages. In this tutorial we use JavaScript.</p>
<p>The programming interface to the DOM is defined by a set standard properties
and methods.</p>
<p><b>Properties</b> are often referred to as something that is (i.e. nodename
is "p").</p>
<p><b>Methods</b> are often referred to as something that is done (i.e. delete
"p").</p>
<hr />
<h2>HTML DOM Properties</h2>
<p>These are some typical DOM properties:</p>
<ul>
<li>x.innerHTML - the inner text value of x (a HTML element)</li>
<li>x.nodeName - the name of x</li>
<li>x.nodeValue - the value of x</li>
<li>x.parentNode - the parent node of x</li>
<li>x.childNodes - the child nodes of x</li>
<li>x.attributes - the attributes nodes of x</li>
</ul>
<p><b>Note:</b> In the list above, x is a node object (HTML element).</p>
<hr />
<h2>HTML DOM Methods</h2>
<ul>
<li>x.getElementByID(<i>id</i>) - get the element with a specified id</li>
<li>x.getElementsByTagName(<i>name</i>) - get all elements with a specified
tag name</li>
<li>x.appendChild(<i>node</i>) - insert a child node to x</li>
<li>x.removeChild(<i>node</i>) - remove a child node from x</li>
</ul>
<p><b>Note:</b> In the list above, x is a node object (HTML element).</p>
<hr />
<h2>innerHTML</h2>
<p>The easiest way to get or modify the content of an element is by using the
innerHTML property.</p>
<p>innerHTML is not a part of the W3C DOM specification. However, it is
supported by all major browsers.</p>
<p>The innerHTML property is useful for returning or replacing the content of
HTML elements (including <html> and <body>), it can also be used to view the
source of a page that has been dynamically modified.</p>
<h2>Example</h2>
<p>The JavaScript code to get the text from a <p> element with the id "intro" in
a HTML document:</p>
<p>txt=document.getElementById("intro").innerHTML</p>
<p>After the execution of the statement, txt will hold the value "Hello World!"
</p>
<p>Explained:</p>
<ul>
<li><b>document</b> - the current HTML document</li>
<li><b>getElementById("intro")</b> - the <p> element with the id "intro"</li>
<li><b>innerHTML</b> - the inner text of the HTML element</li>
</ul>
<p>In the example above, getElementById is a method, while innerHMTL is a property. </p>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_methods">Try it
yourself</a></p>
<hr />
<h2>childNodes and nodeValue</h2>
<p>The W3C DOM specified way of getting to the content of an element works like
this:</p>
<p>The JavaScript code to get the text from a <p> element with the id "intro" in
a HTML document:</p>
<p>txt=document.getElementById("intro").childNodes[0].nodeValue</p>
<p>After the execution of the statement, txt will hold the value "Hello World!"
</p>
<p>Explained:</p>
<ul>
<li><b>document</b> - the current HTML document</li>
<li><b>getElementById("intro")</b> - the <p> element with the id "intro"</li>
<li><b>childNodes[0]</b> - the first child of the <p> element (the text
node)</li>
<li><b>nodeValue</b> - the value of the node (the text itself)</li>
</ul>
<p>In the example above, getElementById is a method, while childNodes and
nodeValue are properties. </p>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_methods2">Try it
yourself</a></p>
<p>In this tutorial we will mostly use the innerHTML property. However, learning
the method above is useful for understanding the tree structure of the DOM and
dealing with XML files.</p>
<hr />
<a href="dom_nodetree.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="dom_nodes_access.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 + -