ALTE DOCUMENTE
|
|||||||||
DHTML Object Model
Contents
Overview
What Is the Object Model?
Why Is Object Model So Important?
Access to All Page Elements through Scripts
Full event model
Instant Page Update
Changing the Text on the Page (content of HTML Document)
Elements and Collections
The all Collection and the children Collection
More Collections
Using Collections
Creating new Collections: The tags Method
Accessing Element Properties
Examining the Element Hierarchy
Getting an Element's Position and Dimensions
Scrolling Elements into View
Examples
Example #1 - DHTML document is a structured arrangement of elements
Exemple # 2 - Individual element objects accessed by index or identifier
Exemple # 3 - The all collection and unknown or invalid start/end tags
Example # 4 - Use of the children collection
Example # 5 - Using collection
Exemple # 6 - Retrieving items from a collection by specifying an identifier
Example # 7 - Property of an invalid value assigned to an attribute
Example # 8 - Using getAttribute and setAttribute
Example # 9 - Using a method to examine what is in a document
Example # 10 - Examining the structural hierarchy of a docum 616c21g ent
Example # 11 - Determining the hierarchy of the elements document
Example # 12 - Determining an Element's Position and Dimensions
Overview
On December 9, 1997, the World Wide Web Consortium (W3C) released for public review the HTML DOM Working Draft, now a W3C recommendation. The purpose of the HTML DOM Recommendation is to specify an API for how Web builders and developers can access the Document Object Model to manipulate page elements and create dynamic effects. The specification states that the DOM should represent every HTML element as an object and every attribute as a property. This is consistent with Microsoft's implementation of the DOM in Microsoft Internet Explorer 4.0, but is a superset of Netscape implementation of Navigator's DOM.
The DHTML DOM allows authors direct, programmable access to the individual components of their Web documents, from individual elements to containers. This access, combined with the event model, allows the browser to react to user input, execute scripts on the fly, and display the new content without downloading additional documents from a server. The document object model puts sophisticated interactivity within easy reach of the average HTML author.
What Is the Object Model?
The object model is the mechanism that makes Dynamic HTML programmable. It doesn't require that authors learn new HTML tags, and it doesn't involve any new authoring technologies. In fact, the object model builds on functionality that authors have used for creating content for previous browsers. In Internet Explorer 3.0it was possible only setting the value for a form element in script, or adding mouseover events to links - that means a limited form of the object model to access HTML with script.
What's different in the current object model is that now every HTML element is programmable, defining the so called element model. This means that every HTML element on the page can have script behind it that can be used to interact with user actions and change the page content dynamically. This event model lets a document react when the user has done something on the page, such as move the mouse over a particular element, press a key, or enter information into a form input. Each event can be linked to a script that tells the browser to modify the content on the fly, without having to go back to the server for a new file. The advantages to this are that authors will be able to create interactive Web sites with fewer pages, and users will not have to wait for new pages to download from Web servers, increasing the speed of their browsing and the performance of the Internet as a whole.
The Dynamic HTML Object Model doesn't define a whole new set of tags or attributes; it makes the tags, attributes, and CSS attributes that you know and love totally programmable. Existing JavaScript Object Models provide access to a small set of elements on the page. Further, only a small subset of their attributes can be modified, and only a small subset of events can be fired from them. Dynamic HTML provides access to all HTML elements and full access to every attribute. Additionally, the set of events that can be accessed for each object is much more complete than that of JavaScript Object Models.
The Dynamic HTML Object Model makes it easier to write code due to event bubbling. Event bubbling is a process by which objects can either handle events or allow events to "bubble up" to the parent object. This capability enables you to write less code to handle events. You can let the parent object handle generic events, so that each object doesn't have to, or provide default actions.
It is important to note that the Dynamic HTML Object Model is entirely language- and paradigm-neutral, which means that a Web page can be controlled via scripting, controls, or Java applets. The Dynamic HTML Object Model has been designed so that you can add Dynamic HTML functionality to pages, and they will still display well in older browsers that don't support that functionality.
Why Is Object Model So Important?
The Dynamic HTML Object Model delivers four key innovations that allow Web authors to create truly dynamic pages:
Access to all page elements through scripts
Instant page update
Full event model
Changing the text on the page
Before we dive deeper, its important to explain a key feature of Dynamic HTML. Changes can be made to the page at any time: before load, during load, and after load, when the user clicks on a button, when the user moves his or her mouse, when it's 12:30 A.M. Whenever.
Access to All Page Elements through Scripts
Using Internet Explorer 3.x and even Netscape browsers, a select set of page elements can be accessed from script. Anchors, forms, applets, form elements, and images all can be accessed in script. However, if you want to build a dynamic table of contents, and you need to go through all of the headings on the page-well, it can't be done. The headings aren't accessible.
With the Dynamic HTML Object Model, this is no longer the case. Every single element on the page is accessible. The document features a collection called the all collection. It contains all of the elements on the page. This collection is indexed by name and ID. In the following chunk of code, I will get the H1-level heading with ID MyH1.
<H1 id=MyH1 style="font-weight: normal">Dynamic HTML</H1>
<script language=JavaScript>
function findMyH1()
</script>
Even though this is relatively easy, there is an even easier way to access elements. Just use their ID or name directly.
<script language=JavaScript>
function findMyH1()
</script>
Every HTML element is a scriptable object in the object model, with its own set of properties, methods, and events. However, to write script for each element object, the author must know how to get to an element.
The object model is focused around collections of elements, a hierarchy of groupings that the elements fall into. The most important of these collections are the all collection and the children collection. A Dynamic HTML document is a structured arrangement of elements-for instance, in the following example, each element has a scope of influence that depends on where in the document the tags appear.
<HTML>
<BODY>
<DIV>
<P>Some text in a <B>paragraph</B>
<IMG id=image1 src="mygif.gif">
</DIV>
<IMG id=image2 src="mygif.gif">
</BODY>
</HTML>
Example #1 - DHTML document is a structured arrangement of elements
The DIV element contains (and is the parent of) the P tag and the IMG called image1. Conversely, image1 and the P are children of the DIV. The IMG tag called image2, however, is a child of the BODY element. And all elements are children of the HTML element.
For each element object, there is an all collection that contains all the elements that are beneath that element in the hierarchy, and a children collection that contains only the elements that are direct descendants of that element. In the example above, the B would be in the DIV's all collection, but would not appear in the DIV's children collection. Similarly, the DIV is a member of BODY's children collection, but the P is not.
In addition to these collections for each element, the document itself (represented by the document object) has a number of element and non-element collections. The most important is an all collection that contains all the elements on the Web page. This collection is the primary way to access elements through script.
Full event model
GUI applications are tied together with their event model. One of the key shortcomings of previous HTML Object Models was that the event model was incomplete. Only a small set of events was available for a select set of tags. With the Dynamic HTML Object Model, all elements source a full set of mouse, keyboard, focus, and specialized events.
To continue with our H1 example, let's extend it so that the heading will italicize when we mouse over it. To do this, I need only to trap the mouseover and mouseout events on my H1 heading.
<H1 id=MyH1 style="font-weight: normal"
onmouseover="makeItalic();"
onmouseout="makeNormal();">
Dynamic HTML
</H1>
<script language=JavaScript>
function makeItalic()
function makeNormal()
</script>
Now, when the mouse moves over the H1 heading, it will italicize; when it moves away, the H1 heading will return to normal.
Instant Page Update
Now that it's possible to get to all of these page elements, what can we do with them? Elements have attributes and styles, which can be modified at any time. To change the font style of my heading, the code is trivial.
<H1 id=MyH1 style="font-weight: normal">Dynamic HTML</H1>
<script language=JavaScript>
function changeMyH1()
</script>
When that script executes, the page instantly is updated. It does not have to be reloaded, it just changes automatically. Any attribute or style that you can specify in HTML can be changed dynamically, at any time, with the Dynamic HTML Object Model.
Changing the Text on the Page (content of HTML Document)
Not only can we change the attributes of elements on the page, we can change the actual HTML on the page, on the fly. There are four interesting properties to enable this:
innerHTML
innerText
outerHTML
outerText
The inner properties only apply to container elements -- such as DIV, SPAN, and H1 -- and can be used to replace the actual HTML inside of a container. The outer properties apply to all HTML tags in the body of the document, and can be used to replace an entire element and its contents.
The innerText and outerText properties return a textual representation of the HTML, without the HTML tag information. The innerHTML and outerHTML properties return the actual HTML string, with all of the embedded HTML information.
Using the innerHTML or outerHTML properties tells Internet Explorer to treat the new string and HTML, and to parse it accordingly. Using the innerText or outerText methods tells Internet Explorer to insert the supplied string literally into the document, without parsing it. These properties give access to the underlying HTML and also the plain text of the elements on the page.
To extend our H1 example even further, we will use the innerHTML property to dynamically change the contents of the H1 heading.
<H1 id=MyH1 style="font-weight: normal" onclick="changeH1();" >
Dynamic HTML
</H1>
<script language=JavaScript>
function changeH1()
</script>
In the above case, we simply pasted plain text into the document, so we used the innerText method. However, if we use the innerHTML property, our inserted text can actually be new HTML tags.
Elements and Collections
Every HTML document consists of a combination of HTML tags and their attributes. These elements define the structure of the document and determine how the content is presented. Using the Dynamic HTML object model, you can examine and modify these elements and their modifying attributes. The following topics explain how to access the elements of the document using element collections.
The all Collection and the children Collection
An HTML document is a hierarchical construct of tags that define the contents of the document. The all collection on the document object represents all the elements in the document hierarchy. Each element is represented as a programmable object appearing within the collection in source order. Individual element objects are accessed by index or identifier (unique name). The following document shows how to do this.
<HTML>
<HEAD><TITLE>Elements: Collecting</TITLE>
<SCRIPT LANGUAGE="JScript">
function showElements()
</SCRIPT>
</HEAD>
<BODY onload="showElements()">
<H1>Welcome!</H1>
<P>This document is <B>very</B> short.
</BODY>
</HTML>
Exemple # 2 - Individual element objects accessed by index or identifier
When this document is loaded, the script (an event handler for the onload event) displays a list of the elements in the document by stepping through the all collection. It displays this message:
This document contains: HTML HEAD TITLE SCRIPT BODY H1 P B
You can see that each HTML element in the document is represented. Notice that the list does not show the end tags for the elements. This is because each element object represents both the start and end tags. Also, the collection does not directly indicate the hierarchy of the elements; that is, you can't tell which elements contain others. The collection lists the elements in the order in which they appear in the HTML source of the document.
In many ways, the all collection is like an array. It contains one or more items each of the same type-in this case, element objects. You access the items by using zero-based index values, or by name or identifier. The first item has index value 0, the second has 1, and so on. You can determine how many items are in the collection by using the length property.
Because each item in the all collection is an element object, you can apply properties and methods to these items. For example, you can use the tagName property to retrieve the HTML tag name of the element as was done in the previous example. Similarly, you can access properties and methods of the respective element by accessing this through the document.all collection.
The all collection always represents the current state of the document and is automatically updated to reflect any changes made to the document. For example, if you retrieve the collection and add or remove document content so that the HTML structure is different, the collection will automatically reflect the new HTML content in source order.
In some cases, the all collection might contain more elements than are actually in the document's file. In particular, the collection always contains the HTML, HEAD, TITLE, and BODY elements even if these are not present in the source. Similarly, the collection always contains a TBODY element for each TABLE regardless of whether TBODY was specified in the HTML source.
The all collection also includes comments (!) and unknown or invalid tags. The purpose is to give you an accurate picture of the content of the document. Unknown or invalid tags are typically misspelled or misplaced tags. Knowing what and where they are gives you an opportunity to replace them with valid tags. The all collection lists unknown and invalid start and end tags separately; it does not attempt to combine these into a single item. The following document displays the message "HTML HEAD TITLE SCRIPT BODY ! P ZZZ> /ZZZ> /B".
<HTML>
<HEAD><TITLE>Elements: Collecting</TITLE>
<SCRIPT LANGUAGE="JScript">
function showElements()
</SCRIPT>
</HEAD>
<BODY onload="showElements()">
<!-- A comment -->
<P>This document has an <ZZZ>unknown</ZZZ> and an invalid</B> tag.
</BODY>
</HTML>
Exemple # 3 - The all collection and unknown or invalid start/end tags
In addition to the all collection on the document object, each individual element also exposes an all collection. Remember the hierarchical style of HTML-this helps to think about the all collection for each element. The all collection for an element contains all the elements contained by that element. For example, the all collection for the HTML element would contain everything in the source code except the HTML element (this being the only difference between the all collection for the HTML element and the document.all collection).
Each element also exposes a children collection, which contains only the elements that are direct descendants of the element in the HTML hierarchy. Another way of saying this is that the children collection would contain only those elements whose parentElement property would return that element. The content of the children collection is undefined for overlapping elements.
The following example returns the length and contents of the all and the children collections of the document. Notice that there are only two elements that are children of the HTML tag-HEAD and BODY. The TITLE and SCRIPT elements are children of the HEAD, not of the HTML tag.
<HTML id=theHTML>
<HEAD>
<TITLE> Look at the all and the children collection</TITLE>
<SCRIPT>
function showme()
alert('children: ' + window.theHTML.children.length);
for (i=0; i < theHTML.children.length;i++)
}
</SCRIPT>
</HEAD>
<BODY onload=showme()>
<DIV>
Some text in a DIV. This DIV will be in the all collection of the HTML element
</DIV>
</BODY>
</HTML>
Example # 4 - Use of the children collection
More Collections
The object model provides additional collections that you can use to access the elements of the document.
Collection |
Applies to |
Contains |
Anchors |
document object |
A elements having non-empty name property |
Applets |
document |
APPLET elements |
Areas |
MAP element |
AREA elements in the map |
Cells |
TR element |
TD or TH elements in the row |
Elements |
FORM element |
BUTTON, INPUT, SELECT, and /or TEXTAREA elements in the form |
Embeds |
document |
EMBED elements |
Filters |
all elements |
filter objects |
Forms |
document |
FORM elements |
Frames |
document and window |
window objects defined with FRAME and IFRAME elements |
Images |
document |
IMG elements |
Links |
document |
A elements having non-empty href properties |
Options |
SELECT element |
OPTION elements |
Rows |
TABLE |
TR elements in the table |
Scripts |
document |
SCRIPT elements |
Tbodies |
TABLE |
TBODY elements |
Some scriptable collections are not really element collections, but collections of other scriptable objects.
Collection |
Applies to |
Contains |
Imports |
styleSheet object |
@import statements that are defined in a style sheet |
StyleSheets |
document |
styleSheet objects- represent instances of LINK and STYLE |
Filters |
element |
collection of CSS-defined filter attributes per element object |
Rules |
styleSheet object |
collection of style rules-combination of a CSS contextual selector and a style |
Like the all collection, these collections are automatically updated as changes are made to the document.
Using Collections
Collections in the object model are similar to an array of variables created in a programming language. You can retrieve an item from the collection by providing an name or identifier for the item, or an ordinal index that specifies the position of the item in the collection. Collections indexes are zero-based, so the first item always has an index of zero. The following JScript example displays the tag name of the P element, the sixth element in the collection.
<HTML>
<HEAD><TITLE>Elements: Retrieving Items</TITLE>
<SCRIPT LANGUAGE="JScript">
function showSixth()
</SCRIPT>
</HEAD>
<BODY onload="showSixth()">
<P>This is a very simple document.
</BODY>
</HTML>
Example # 5 - Using collection
Because index values for collections are zero-based, the index for the last element in a collection is always one less than the length of the collection. So a quick way to get access to the last element in a collection is to subtract one from its length and use the result as the index value, as in the following example.
var elLast = document.all(document.all.length-1);
alert("The last element in this document is: " + elLast.tagName);
You can also retrieve items from a collection by specifying an identifier. In this case, the element must have a valid identifier, set using the ID= attribute. The following JScript example displays the tag name of the element that has the identifier "MyID".
<HTML>
<HEAD><TITLE>Elements: IDs as Indexes</TITLE>
<SCRIPT LANGUAGE="JScript">
function showElementWithID()
</SCRIPT>
</HEAD>
<BODY onload="showElementWithID()">
<P ID="MyID">This is a very simple document.
</BODY>
</HTML>
Exemple # 6 - Retrieving items from a collection by specifying an identifier
Elements can also be accessed by name, but it is important to remember that the name attribute primarily applies only to the elements that participate in a form submission. The ID attribute is an identifier that applies to every element.
Be careful! While in theory ID means "unique identifier," in reality the uniqueness is not enforced. If more than one element has the same identifier, the result is a collection of elements with the same ID, rather than an individual element. The subsequent collection can only be accessed ordinally. If no element has the identifier, the result is null.
When using an identifier results in another collection, the elements in the new collection have the same order as they have in the document. To get access to these elements, you can use the item method to apply a zero-based index to the collection, as in the following example.
if (document.all("MyID").length != null)
alert("The first element that has the identifier MyID: " +
document.all("MyID").item(0).tagName) ;
If you already know that more than one element has the same identifier, you can skip a step and use the optional second parameter of item to retrieve an element without first retrieving the collection. The following JScript example displays the same message as the previous example.
var el = document.all.item("MyID",0);
if (el != null)
alert("The first element that has the identifier MyID: " + el.tagName);
If you already know that more than one element has the same identifier, you can skip a step and use the optional second parameter of item to retrieve an element without first retrieving the collection. The following JScript example displays the same message as the previous example.
var el = document.all.item("MyID",0);
if (el != null)
alert("The first element that has the identifier MyID: " + el.tagName);
When using scripting languages such as JScript and VBScript (Visual Basic® Scripting Edition), you can also access elements in collections by using the indexing syntax for that language. JScript offers three ways to index an array: an integer index enclosed in square brackets, an identifier given as a string enclosed in square brackets, and an identifier prefixed with a period. The following JScript statements show how to apply these.
var el = document.all[2]; // is the same as document.all.item(2)
var el = document.all["MyID"]; // is the same as document.all.item("MyID")
var el = document.all.MyID; // is the same as document.all.item("MyID")
Again, be careful when using identifiers in this way. The collection returns another collection if there is more than one element having the given identifier. Always check the return value before you use it.
You can't use identifiers to access items in an imports collection-the collection consists of imported style sheets rather than HTML elements, so no identifiers are available. Use integer indexes instead. Although you can use names to access items in a frames collection, you should make certain that no two windows have the same name. If you don't, only the first window with the given name will be accessible.
Creating new Collections: The tags Method
The tags method creates a collection of elements that have a given tag name. The method filters an existing collection and creates a new collection of a given tag name. For example, the following JScript code applies the tags method to the all collection to retrieve a new collection containing only the TABLE elements in the document. It then applies a border to each table.
var doc_tables = document.all.tags("TABLE");
for (i=0; i<doc_tables.length; i++)
doc_tables(i).border = 1;
The method searches for any tag name, even names that are not valid HTML. If it finds one or more elements having that name, it returns the collection. If it can't find an element having that name, it returns an empty collection. Use the length property to determine how many elements the collection contains. The collection is empty if its length is zero.
The tags method preserves the order of the elements as it creates the new collection. This means the first element in the new collection is also the first instance of the tag in the document.
Because the collection returned by the tags method is a subset of the all collection, the ordinal value that you use to access an element is different than you would use to access an element in the all collection. Sometimes it is useful to know both ordinal values. The sourceIndex property for the element returns the ordinal position of the element in the document's all collection.
Accessing Element Properties
Many element properties are named-value pairs that correspond to the HTML attributes of the element and to other values associated with the element. You can get the value of a property to determine how the corresponding attribute is set, and in many cases change the value of this property to dynamically affect the element. This is the key concept behind manipulating dynamic styles and dynamic content. Accessing the properties that let you change styles and content is done by accessing the element object through the collections exposed in the document object model. For example, you can center an H1 element by accessing the H1 element through the all collection on the document and then setting its align property to "center", the same value you would use with the ALIGN= attribute.
var coll = document.all.tags("H1");
if (coll.length>0)
coll(0).align="center";
Similarly, you can change the image file name for an IMG element by setting its src property.
var coll = document.all.tags("IMG");
if (coll.length>0)
coll(0).src="newimage.gif";
Most properties have the same name and take the same values as the corresponding attribute. This means most properties take strings, enumerated values, or numeric values. If a property name is different from its corresponding attribute, it is usually fairly close to the attribute name. For example, the className property corresponds to the CLASS= attribute. Name changes are done to avoid conflicts with keywords in common scripting languages, or to make sure that property names don't contain characters that are invalid in common scripting languages.
A few properties represent sub-objects. For example, style corresponds to the STYLE= attribute and represents a sub-object that gives you access to all the CSS attributes that can be applied to the element as an inline style. The style object itself takes properties, which you can set to alter the look and formatting of the element. The following example uses the style object to change the CSS color attribute of all H1 elements to green.
var coll = document.all.tags("H1");
for (i=0; i<coll.length; i++)
coll[i].style.color = "green";
Some properties do not correspond to element attributes. These properties typically give additional information about the element that isn't available through attributes. For example, the tagName property specifies the HTML tag name of the element, and the sourceIndex property specifies the position of the element in the document's all collection.
Although you can get and set the values of many properties, some properties are "read-only" in that you can get the values but not set or change them. For example, the tagName property is a read-only property-changing the tag name of an element is not permitted.
Sometimes an HTML document will have invalid attributes, or invalid values assigned to attributes. If an invalid value is assigned to an attribute, the corresponding property is always set to its default value. For example, in the following document, "middle" is not a valid value for the ALIGN= attribute, so the document displays the message "left", the default value for the align property.
<HTML>
<HEAD><TITLE>Elements: Using Properties</TITLE>
<SCRIPT LANGUAGE="JScript">
function showAlignment()
</SCRIPT>
</HEAD>
<BODY onload="showAlignment()">
<H1 ALIGN="middle">Welcome!</H1>
<P>This document is <B>very</B> short.
</BODY>
</HTML>
Example # 7 - Property of an invalid value assigned to an attribute
The getAttribute, setAttribute, and removeAttribute methods give you access to the attributes of an element without using properties. You supply an attribute name, and these methods return or set the current value of the attribute, or remove the attribute.
For example, you can get and set the current value of the ALIGN= attribute (rather than retrieve the align property) by using getAttribute and setAttribute as in the following document. The document displays the current value of the attribute ("left"), then changes that value to "center", centering the H1 element.
<HTML>
<HEAD><TITLE>Elements: Using Methods</TITLE>
<SCRIPT LANGUAGE="JScript">
function showAndSetAlignment()
</SCRIPT>
</HEAD>
<BODY onload="showAndSetAlignment()">
<H1 ID=MyHeading ALIGN="left">Welcome!</H1>
<P>This a short document.
</BODY>
</HTML>
Example # 8 - Using getAttribute and setAttribute
The attribute methods are especially useful when working with unrecognized attributes and invalid values. For example, if an attribute has an invalid value, the getAttribute method retrieves that exact value rather than the default value for the attribute. This means you can use the method to examine what is really in the HTML document and make decisions based on that. The following document uses the methods to repair the alignment of the H1 element ("middle" is not a valid value).
<HTML>
<HEAD><TITLE>Elements: Invalid Values</TITLE>
<SCRIPT LANGUAGE="JScript">
function checkValues()
</SCRIPT>
</HEAD>
<BODY onload="checkValues()">
<H1 ID=MyHeading XyZ="123" ALIGN="middle">Welcome!</H1>
<P>This is a short document.
</BODY>
</HTML>
Example # 9 - Using a method to examine what is in a document
By default, these attribute methods are case insensitive when looking for an unknown attribute. So in the example above, even though "xyz" isn't identical to "XyZ", the methods get and set the attribute anyway. You can force the methods to search for an exact match by setting the optional case-sensitive parameter to true. For example, the following code, if placed in the previous example, displays "Not found" because "xyz" does not match "XyZ".
if (document.all.MyHeading.getAttribute("xyz", true)==null)
alert("Not found");
If multiple matches are found for a case-sensitive usage of these methods, the actual item returned is not guaranteed to be consistent across platforms.
Examining the Element Hierarchy
The elements of an HTML document form a hierarchy in which some elements contain others. The HTML element, at the top of the hierarchy, contains all other elements in the document. The BODY element, contained by HTML, represents the document. Block elements, such as P, contain text and inline elements, which can themselves contain text and inline elements, and so on.
You can examine this structural hierarchy by using the contains method and the parentElement property. The all collection and the children collection (discussed previously) for each element can also help determine the document structure.
The contains method returns either true or false to indicate whether one element contains another. You apply the method to an element and supply another element as the parameter. For example, the following document displays the message "True!" because the BODY element contains P.
<HTML>
<HEAD><TITLE>Elements: Hierarchy</TITLE>
<SCRIPT LANGUAGE="JScript">
function showContains()
</SCRIPT>
</HEAD>
<BODY onload="showContains()">
<P>This is a very simple document.
</BODY>
</HTML>
Example # 10 - Examining the structural hierarchy of a docum 616c21g ent
Note that the body property of the document object is used to retrieve the BODY object.
The parentElement property always identifies the next element in the hierarchy that contains the element. You can combine contains and parentElement in a more complicated script to determine the hierarchy of the elements document. The following document uses these to determine the hierarchy, then it displays a message that shows the hierarchy.
<HTML>
<HEAD><TITLE>Elements: Hierarchy</TITLE>
<SCRIPT LANGUAGE="JScript">
function showHierarchy() else
}
msg = msg + "\n";
for (j=1; j<=depth; j++)
msg = msg + " ";
msg = msg + document.all(i).tagName;
}
alert("This document contains:\n" + msg);
}
</SCRIPT>
</HEAD>
<BODY onload="showHierarchy()">
<H1>Welcome!</H1>
<P>This document is <B>very</B> short.
</BODY>
</HTML>
Example # 11 - Determining the hierarchy of the elements document
Getting an Element's Position and Dimensions
You can determine the location, width, and height of an element by using the offsetLeft, offsetTop, offsetHeight, and offsetWidth properties. These numeric properties specify the physical coordinates and dimensions of the element relative to the element's offset parent. For example, the following document is a simple clock that adjusts the size of its readout to fit the current width and height of the document body.
<HTML>
<HEAD><TITLE>A Simple Clock</TITLE>
<SCRIPT LANGUAGE="JScript">
function startClock()
var ratio = 4;
function Clock_Tick()
</SCRIPT>
</HEAD>
<BODY onload="startClock()">
<P ID="MyTime"> </P>
</BODY>
</HTML>
Example # 12 - Determining an Element's Position and Dimensions
The offsetLeft and offsetTop property values are relative to the element specified by the offsetParent property for the element. Most of the time the property returns BODY. For example, in the following document, even though the TD element appears to the far right in the document, its position is (0,0) because its offset parent is the TR, not the document body.
<HTML>
<HEAD><TITLE>Elements: Positions</TITLE>
<SCRIPT LANGUAGE="JScript">
function showPosition()
</SCRIPT>
</HEAD>
<BODY onload="showPosition()">
<P>This document contains a right-aligned table.
<TABLE BORDER=1 ALIGN=right>
<TR><TD ID=MyID>This is a small table
</TABLE>
</BODY>
</HTML>
Example # 13 - Using offsetLeft and offsetTop property
Scrolling Elements into View
Another useful method is scrollIntoView. This scrolls the element into view within the window, placing it at either the top or bottom of the window. The method is useful for immediately showing the user the result of some action without requiring the user to manually scroll the document to find the result. The following example underlines the content of the fifth paragraph and scrolls it into view at the top of the window.
var coll = document.all.tags("P");
if (coll.length>=5)
|