當瀏覽器讀入一頁網頁時,就會根據此網頁的內容來建立相關的文件物件模型(Document Object Model ,簡稱 DOM),我們需要瞭解這些物件模型的性質和方法,才能產生動態的網頁,充分利用 Dhtml(Dynamical html)的各種功能。
在文件物件模型中,所有的物件之間有一個階層式的關係,請見下表:
這些物件各有不同的屬性及方法,JavaScript 可利用這些屬性及方法來建立網頁的互動性。
首先我們必須知道如何取得物件的屬性,並進而改變這些屬性,才能產生動態網頁。欲存取物件的屬性,有下列三種方法:
<script language="JavaScript"> <!-- document.alinkColor="#FF0000"; //--> </script>
<body alink="red"> <h1>My Super Duper Web Page</h1> <script language="JavaScript"> <!-- document.write("The active link color here is " + document.alinkColor); //--> </script> <P> <A HREF="bgcolor.htm">Try a link </body>
例題:alink.html
<h2><a id="books" name="books"></a>Textbook</h2> <pre> <a href="http://www.pageresource.com/jsbook.html">JavaScript: A Beginner's Guide</a>, by John Pollock, McGraw-Hill, 2001. </pre> <hr /> <h3><a name="refs"></a>On-line References</h3> <ul> <li><a href="http://www.csie.ntu.edu.tw/~sylee/courses/webprog/">Course Home Page</a></li> <li><a href="http://www.pageresource.com/jscript/">Pageresource Javascript</a></li> <li><a href="http://www.wsabstract/">Javascript Scripts</a></li> <li><a href="http://www.javascriptcity.com/java/">Javascript Forums</a></li> </ul> <hr /> <h3><a name="instr">Instructor: <a href="http://www.csie..ntu.edu.tw/~sylee/">Prof. S. Y. Lee</a></h3> <hr /> <script language="JavaScript"> <!-- document.write("There are " + document.anchors.length + " named anchors."); //--> </script>
例題:anchors.htm
<head> <script language="JavaScript"> <!-- document.bgColor="lightblue"; //--> </script> </head>
<body bgColor="yellow"> <h1>My Super Duper Web Page</h1> <script language="JavaScript"> <!-- document.write("The background color here is " + document.bgColor); //--> </script> </body>
<body bgColor="red" text="white"> <h1>My Super Duper Web Page</h1> <B>Want a new background color? Change it to light blue by clicking the button below!</B> <form> <input type="button" value="Click" onClick="document.bgColor='lightblue';"> </form> </body>示範: bgcolor.htm
<html> <head> <title>Background Colors</title> <script language="JavaScript"> <!-- function newbg(thecolor) { document.bgColor=thecolor; } //--> </script> </head> <body bgColor="yellow"> <B>Want a new background color? Change the color by clicking the button below!</B> <form> <input type="button" value="淺藍" onClick="newbg('lightblue');"> <input type="button" value="橘黃" onClick="newbg('orange');"> <input type="button" value="米黃" onClick="newbg('beige');"> <input type="button" value="黃" onClick="newbg('yellow');"> </form> </body> </html>示範: background 2
<script language="JavaScript"> <!-- document.cookie="site=homepage;food=cheeseburgers"; //--> </script>
<script language="JavaScript"> <!-- window.alert("You have reached the " + document.domain + " domain!"); //--> </script>
<head> <script language="JavaScript"> <!-- document.fgColor="blue"; //--> </script> </head> <body text="blue"> <h1>My Super Duper Web Page</h1> <script language="JavaScript"> <!-- document.write("The text color here is " + document.fgColor); //--> </script> </body>
<body> <h2>Forms and Names</h2> <form name="form1"> <input type="button" name="but" value="You can click me I suppose" onClick="document.form1.but='Thanks for clicked me!';"> </form> </body>
<html> <head> <title>使用屬性document.images用法之一</title> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <script> function mytest() { var returnData = ""; var imgs = document.images; for (i = 0; i < imgs.length; i++) { returnData = returnData + imgs[i].id + ","; } document.getElementById("returnData").innerHTML = returnData; } </script> </head> <body> <br> <img id="smiley1" border="0" src="../../../gif/smiley.gif" width="150" height="113"> <img id="smiley2" border="0" src="../../../gif/smiley.gif" width="152" height="128"> <input type="button" value="測試" onclick="mytest()" /> <span id="returnData"></span> </body> </html>
<html> <head> <script> function changeLink(){ document.getElementById('myAnchor').innerHTML="W3Cschool"; document.getElementById('myAnchor').href="https://www.w3schools.com/js/js_htmldom.asp"; document.getElementById('myAnchor').target="_blank"; } </script> </head> <body> <a id="myAnchor" href="http://www.microsoft.com">Microsoft</a> <input type="button" onclick="changeLink()" value="Change link"> </body> </html>
<body> <h2>Last Modified Date</h2> document.write("Last modified: " + document.lastModified); </body>
<script language="JavaScript"> <!-- if (document.layers) { window.alert("You have Netscape Navigator 4 or better"); } //--> </script>
<html> <head> <title>all屬性用法</title> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <script language="JavaScript"> <!-- var elements = ""; function start() { for (var i = 0; i < document.all.length; ++i) elements += "<br />" + document.all[i].tagName; pText.innerHTML += elements; alert(elements); } //--> </script> </head> <body onLoad = "start()"> <p id="pText">網頁上所有的元素</p> </body> </html>
<script language="JavaScript"> <!-- document.linkColor="#0000FF"; //--> </script>
<body alink="red" link="blue"> <h1>My Super Duper Web Page</h1> <script language="JavaScript"> <!-- document.write("The link color here is " + document.linkColor); document.write("<P>The active link color here is " + document.alinkColor); //--> </script> <P> <A HREF="bgcolor.htm">Try a link </body>示範: linkColor.html
<script language="JavaScript"> <!-- document.write("You came from " + document.referrer + "!"); //--> </script>示範: referrer.htm
<html> <head> <title>Introduction to Web Programming</title> </head> <body> <script language="JavaScript"> <!-- document.write("<H1>" + document.title + "</H1>"); //--> </script> <h2 align="center">Spring 2003</h2> </body>示範: title
<html> <head> <title>Introduction to Web Programming</title> </head> <body> <script language="JavaScript"> <!-- document.write("<H1>" + document.title + "</H1>"); //--> </script> <h2 align="center">Spring 2003</h2> <script language="JavaScript"> <!-- document.write("You are at: " + document.URL + "."); //--> </script> </body>示範: URL
<html> <head> <script language="JavaScript"> <!-- function newpage() { var thename=document.myform.yourname.value; document.open(); document.write("<h1>Welcome!</h1>"); document.write("Hello, " + thename + ", and welcome to my page!"); document.close(); } //--> </script> </head> <body> <b>Enter your name in the box below, then click the button to see a personalized page!</b> <p> <form name="myform"> Name: <input type="text" name="yourname" size="25"> <p> <input type="button" value="Click" onClick="newpage();"> </form> </body> </html>示範: open/close
輸入資料的檢查
<script language="JavaScript"> <!-- if ( (thename=="") || (thename==null) ) { window.alert("Enter something, then try again!"); } //--> </script>示範: open/close with data check