Java Reference
In-Depth Information
Each of these objects inherits all the properties and methods of the Node object, but also has some prop-
erties and methods of its own. You will be looking at some examples in the next section.
DOM Objects and Their Properties and Methods
If you tried to look at the properties and methods of all the objects in the DOM, it would take up half
the topic. Instead you're going to actively consider only three of the objects, namely the Node object, the
Element object, and the Document object. This is all you'll need to be able to create, amend, and navi-
gate your tree structure. Also, you're not going to spend ages trawling through each of the properties
and methods of these objects, but rather look only at some of the most useful properties and methods and
use them to achieve specifi c ends.
Appendix C contains a relatively complete reference to the DOM, its objects, and their properties.
The Document Object and its Methods
The Document reference type exposes various properties and methods that are very helpful to someone
scripting the DOM. Its methods allow you to fi nd individual or groups of elements and create new elements,
attributes, and text nodes. Any DOM scripter should know these methods and properties, as they're
used quite frequently.
The Document object's methods are probably the most important methods you'll learn. While many
tools are at your disposal, the Document object's methods let you fi nd, create, and delete elements in
your page.
Finding Elements or an Element
Let's say you have an HTML web page — how do you go about getting back a particular element on the
page in script? The Document reference type exposes the follow methods to perform this task:
Methods of the Document Object
Description
getElementById(idValue)
Returns a reference (a node) to an element, when supplied
with the value of the id attribute of that element
getElementsByTagName(tagName)
Returns a reference (a node list) to a set of elements that
have the same tag as the one supplied in the argument
The fi rst of the two methods, getElementById() , requires you to ensure that every element you want
to quickly access in the page uses an id attribute, otherwise a null value (a word indicating a missing
or unknown value) will be returned by your method. Let's go back to the fi rst example and add some
id attributes to the elements.
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”><head>
<title>example</title>
</head>
Search WWH ::




Custom Search