ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: HTML DOM :: HTML DOM ¾ÙÀý

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

HTML DOM
DOM TableRow ¶ÔÏ..
DOM Textarea ¶ÔÏ..
DOM Window ¶ÔÏó

HTML DOM ¾ÙÀý


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ä¯ÀÀ:8345 :: ::

The HTML DOM defines an HTML document as a collection of objects.
HTML DOM¶¨ÒåHTMLÎĵµ×÷Ϊһ¸ö¶ÔÏóµÄ¼¯ºÏ

Objects have properties and methods. Objects can respond to events.
¶ÔÏóÓÐÊôÐԺͷ½·¨¡£¶ÔÏó¿ÉÒÔ¶Ôʼþ×ö³ö·´Ó¦


An HTML DOM Example
Ò»¸öHTML DOMÀý×Ó

This coding example shows how the background color of an HTML document can be changed to yellow when a user clicks on it:
Àý×ÓÖеĴúÂëչʾÁËÔõÑùÀ´ÓÃÊó±êµã»÷¸Ä±äHTMLÎĵµµÄ±³¾°ÑÕɫΪ»ÆÉ«£º

<html>

<head>
<script type="text/javascript">

function ChangeColor()
{
document.body.bgColor="yellow"
}
</script>
</head>

<body onclick="ChangeColor()">
Click on this document!
</body>

</html>

Try it yourself×Ô¼º³¢ÊÔÏÂ


Document Objects
Îĵµ¶ÔÏó

The HTML DOM defines HTML documents as a collection of objects.
HTML DOM ½« HTML Îĵµ¶¨Òå³É¶ÔÏóÃǵļ¯ºÏ

The document object is the parent of all the other objects in an HTML document.
document¶ÔÏóÊÇËùÓÐHTMLÎĵµÄÚÆäËû¶ÔÏóµÄ¸¸½Úµã

The document.body object represents the <body> element of the HTML document.
document.body¶ÔÏó´ú±íÁËHTMLÎĵµµÄ<body>ÔªËØ

The document object is the parent of the body object, and the body object is a child of the document object.
document ¶ÔÏóÊÇbody¶ÔÏóµÄ¸¸½Úµã£¬Ò²¿ÉÒÔ˵body¶ÔÏóÊÇdocument¶ÔÏóµÄ×Ó½Úµã


Object Properties
¶ÔÏóÊôÐÔ

HTML document objects can have properties (also called attributes).
HTMLµÄdocument¶ÔÏó¿ÉÒÔÓÐÊôÐÔ(Ò²¿ÉÒÔ½ÐΪattributes[ÊôÐÔ])

The document.body.bgColor property defines the background color of the body object.
document.body.bgColorÊôÐÔ¶¨ÒåÁËbody¶ÔÏóµÄ±³¾°ÑÕÉ«

The statement document.body.bgColor="yellow" in the example above, sets the background color of the HTML document to yellow.
ÔÚÉÏÃæÀý×ÓÖеÄÓï¾ädocument.body.bgColor="yellow"½«HTMLÎĵµµÄ±³¾°ÑÕÉ«ÉèÖÃΪÁË»ÆÉ«¡£


Object Events
¶ÔÏóʼþ

HTML document objects can respond to events.
HTMLÎĵµ¶ÔÏó¿ÉÒÔ¶Ôʼþ×ö³ö·´Ó¦

The onclick="ChangeColor()" attribute of the <body> element in the example above, defines an action to take place when the user clicks on the document.
ÔÚÉÏÃæÀý×ÓÖÐ<body>ÔªËØµÄonclick="ChangeColor()"ÊôÐÔ¶¨ÒåÁ˵±Óû§µã»÷Îĵµºó·¢Éú£¨ÏàÓ¦µÄ£©¾Ù¶¯


Functions
º¯Êý

The ChangeColor() function in the example above, is a JavaScript function.
Àý×ÓÖеÄChangeColor()º¯ÊýÊÇÒ»¸öJSº¯Êý

The function will be triggered (started) when a user clicks on the HTML document.
µ±Óû§µã»÷HTMLÎĵµº¯Êý¾Í»á±»´¥·¢£¨¿ªÊ¼£©


Changing Style
¸Ä±äÑùʽ

The HTML DOM also defines a model for changing the styles of HTML objects.
HTML DOM»¹¿ÉÒÔ¶¨ÒåHTML¶ÔÏóµÄÑùʽģÐÍ

The coding example below shows how to obtain the same effect as the first example, by changing the style of the body object:
ÏÂÃæµÄ´úÂëչʾÁËÔõÑù×öµ½ºÍÉÏÒ»¸öÀý×ÓÒ»ÑùµÄЧ¹û¶øÊ¹ÓøıäÑùʽµÄ·½·¨£º

<html>

<head>
<script type="text/javascript">
function ChangeColor()
{
document.body.style.background="yellow"
}
</script>
</head>

<body onclick="ChangeColor()">

Click on this document!
</body>

</html>

Try it yourself×Ô¼º³¢ÊÔÏÂ

ÆÀÂÛ (2) 1 All