📄 dom_using.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 Using the DOM</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 - How to Change HTML Elements</h1>
<a href="dom_nodes_info.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" /></a>
<a href="dom_events.asp"><img border="0" alt="next" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">HTML elements are changed using JavaScript, the HTML DOM and
events.</p>
<hr />
<h2>Changing an HTML Element</h2>
<p>The HTML DOM and JavaScript can be used to change the inner content and attributes of HTML
elements dynamically.</p>
<h2>Example 1 - Change the Background Color</h2>
<p>The following example changes the background color of the <body> element:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table1">
<tr>
<td>
<pre><html>
<body>
<script type="text/javascript">
document.body.bgColor="yellow";
</script>
</body>
</html></pre>
</td>
</tr>
</table>
<p>
<a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_changebackground">Try it
yourself</a></p>
<hr />
<h2>Changing the Text HTML Element - innerHTML</h2>
<p>The easiest way to get or modify the content of an element is by using the
innerHTML property.</p>
<h2>Example 2 - Change the Text of an Element</h2>
<p>The following example changes the text of the <p> element:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table4">
<tr>
<td>
<pre><html>
<body>
<p id="p1">Hello World!</p>
<script type="text/javascript">
document.getElementById("p1").innerHTML="New text!";
</script>
</body>
</html></pre>
</td>
</tr>
</table>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_changetext">Try it
yourself</a></p>
<hr />
<h2>Changing an HTML Element Using Events</h2>
<p>An event handler allows you to execute code when an event occurs.</p>
<p>Events are generated by the browser when the user clicks an element, when the
page loads, when a form is submitted, etc.</p>
<p>You can read more about events in the next chapter.</p>
<h2>Example 3 - Change the Background Color Using the onclick Event</h2>
<p>The following example changes the background color of the <body> element when
the button is clicked:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
<tr>
<td>
<pre><html>
<body></pre>
<pre><input type="button" onclick="document.body.bgColor='yellow';"
value="Click me to change background color"></pre>
<pre></body>
</html></pre>
</td>
</tr>
</table>
<p>
<a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_changebackground2">Try it
yourself</a></p>
<h2>Example 4 - Create a Function to Change the Text of an Element</h2>
<p>The following example uses a function to change the text of the <p> element
when the button is clicked:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table2">
<tr>
<td>
<pre><html>
<head>
<script type="text/javascript">
function ChangeText()
{
document.getElementById("p1").innerHTML="New text!";
}
</script>
</head></pre>
<pre><body>
<p id="p1">Hello world!</p>
<input type="button" onclick="ChangeText()"
value="Click me to change text above">
</body>
</html></pre>
</td>
</tr>
</table>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_changetext2">Try it
yourself</a></p>
<hr />
<h2>Using the Style Object</h2>
<p>The Style object represents of each HTML element represents its individual
style.</p>
<p>The Style object can be accessed from the document or from the elements to
which that style is applied.</p>
<h2>Example 5 - Change the Background Color</h2>
<p>The following example uses a function to change the style of the <body>
element when the button is clicked:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table7">
<tr>
<td>
<pre><html>
<head>
<script type="text/javascript">
function ChangeBackground()
{
document.body.style.backgroundColor="yellow";
}
</script>
</head></pre>
<pre><body>
<p id="p1">Hello world!</p>
<input type="button" onclick="ChangeBackground()"
value="Click me to change background color">
</body>
</html></pre>
</td>
</tr>
</table>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_changestyle">Try it
yourself</a></p>
<h2>Example 6 - Change the Text font and color of an Element</h2>
<p>The following example uses a function to change the style of the <p> element
when the button is clicked:</p>
<table class="ex" cellspacing="0" border="1" width="100%" id="table8">
<tr>
<td>
<pre><html>
<head>
<script type="text/javascript">
function ChangeText()
{
document.getElementById("p1").style.color="blue";
document.getElementById("p1").style.fontFamily="Arial";
}
</script>
</head></pre>
<pre><body>
<p id="p1">Hello world!</p>
<input type="button" onclick="ChangeText()"
value="Click me to change text above">
</body>
</html></pre>
</td>
</tr>
</table>
<p><a target="_blank" href="../js/tryit.asp@filename=try_dom_tut_changestyle2">Try it
yourself</a></p>
<hr />
<a href="dom_nodes_info.asp"><img border="0" alt="previous" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="dom_events.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 + -