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

📄 dom_using.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>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 &lt;body&gt; element:</p>

<table class="ex" cellspacing="0" border="1" width="100%" id="table1">
  <tr>
    <td>
    <pre>&lt;html&gt;
&lt;body&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
document.body.bgColor=&quot;yellow&quot;;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</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 &lt;p&gt; element:</p>

<table class="ex" cellspacing="0" border="1" width="100%" id="table4">
  <tr>
    <td>
    <pre>&lt;html&gt;
&lt;body&gt;
&lt;p id=&quot;p1&quot;&gt;Hello World!&lt;/p&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
document.getElementById(&quot;p1&quot;).innerHTML=&quot;New text!&quot;;
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</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 &lt;body&gt; element when 
the button is clicked:</p>

<table class="ex" cellspacing="0" border="1" width="100%" id="table3">
  <tr>
    <td>
    <pre>&lt;html&gt;
&lt;body&gt;</pre>
	<pre>&lt;input type=&quot;button&quot; onclick=&quot;document.body.bgColor='yellow';&quot;
value=&quot;Click me to change background color&quot;&gt;</pre>
	<pre>&lt;/body&gt;
&lt;/html&gt;</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 &lt;p&gt; element 
when the button is clicked:</p>

<table class="ex" cellspacing="0" border="1" width="100%" id="table2">
  <tr>
    <td>
    <pre>&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function ChangeText()
{
document.getElementById(&quot;p1&quot;).innerHTML=&quot;New text!&quot;;
}
&lt;/script&gt;
&lt;/head&gt;</pre>
	<pre>&lt;body&gt;
&lt;p id=&quot;p1&quot;&gt;Hello world!&lt;/p&gt;
&lt;input type=&quot;button&quot; onclick=&quot;ChangeText()&quot;
value=&quot;Click me to change text above&quot;&gt;
&lt;/body&gt;
&lt;/html&gt;</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 &lt;body&gt; 
element when the button is clicked:</p>

<table class="ex" cellspacing="0" border="1" width="100%" id="table7">
  <tr>
    <td>
    <pre>&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function ChangeBackground()
{
document.body.style.backgroundColor=&quot;yellow&quot;;
}
&lt;/script&gt;
&lt;/head&gt;</pre>
	<pre>&lt;body&gt;
&lt;p id=&quot;p1&quot;&gt;Hello world!&lt;/p&gt;
&lt;input type=&quot;button&quot; onclick=&quot;ChangeBackground()&quot;
value=&quot;Click me to change background color&quot;&gt;
&lt;/body&gt;
&lt;/html&gt;</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 &lt;p&gt; element 
when the button is clicked:</p>

<table class="ex" cellspacing="0" border="1" width="100%" id="table8">
  <tr>
    <td>
    <pre>&lt;html&gt;
&lt;head&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
function ChangeText()
{
document.getElementById(&quot;p1&quot;).style.color=&quot;blue&quot;;
document.getElementById(&quot;p1&quot;).style.fontFamily=&quot;Arial&quot;;
}
&lt;/script&gt;
&lt;/head&gt;</pre>
	<pre>&lt;body&gt;
&lt;p id=&quot;p1&quot;&gt;Hello world!&lt;/p&gt;
&lt;input type=&quot;button&quot; onclick=&quot;ChangeText()&quot;
value=&quot;Click me to change text above&quot;&gt;
&lt;/body&gt;
&lt;/html&gt;</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 + -