w3pop.com :: ÍøÂçѧԺ :: HTML DOM :: HTML DOM ¾ÙÀý
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.
¶ÔÏóÓÐÊôÐԺͷ½·¨¡£¶ÔÏó¿ÉÒÔ¶Ôʼþ×ö³ö·´Ó¦
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> |
Try it yourself×Ô¼º³¢ÊÔÏÂ
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¶ÔÏóµÄ×Ó½Úµã
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ÎĵµµÄ±³¾°ÑÕÉ«ÉèÖÃΪÁË»ÆÉ«¡£
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()"ÊôÐÔ¶¨ÒåÁ˵±Óû§µã»÷Îĵµºó·¢Éú£¨ÏàÓ¦µÄ£©¾Ù¶¯
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Îĵµº¯Êý¾Í»á±»´¥·¢£¨¿ªÊ¼£©
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> |
Try it yourself×Ô¼º³¢ÊÔÏÂ