- 122 -
Type
: Node
Description
: This property represents the first child of the node.
If the node does not
contain any children, then this property is set to null.
Comments
: This property is just a short cut for accessing the first item in a NodeList
collection.
In other words, myNode.firstChild and myNode.childNodes.item(0) represent
the same node.
lastChild
Type
: Node
Description
: This property represents the last child of the node. If the node does not
contain any children, then this property is set to null.
Comments
: This property is a short cut for accessing the
last item in a NodeList
collection. In other words, myNode.lastChild and myNode.childNodes.item(
myNode.childNodes.length - 1 ) represent the same node.
Example
:
Both alerts from the above example produce element3. The first alert comes from using
the lastChild property. The second alert finds the number
of elements in the collection;
subtracts 1 because the collection is indexed from 0, then goes to that item. Both
methods go to the last child in the collection.
previousSibling
Type
: Node
Description
: This property represents a node that has a common
parent and is the child
of this parent that precedes the current node. If this child does not exist, then this
property is set to null.
nextSibling
Type
: Node
- 123 -
Description
: This property represents a node that has a common parent and is the child
of this parent that follows the current node. If this child does not exist, then this property
is set to null.
ownerDocument
Type
:
Document node
Description
: This property represents the Document node of the DOM tree in which the
current node resides.
Comments
: The Document node is the highest level parent for any node, and it always
exists. The ownerDocument property is useful because it is
a shortcut to the node that
has special capabilities to create other nodes. A detailed description of the Document
node is included in this chapter.
Node Methods
Most of the methods described in this section are used to manage and manipulate
children of nodes. Not all nodes—for example, text nodes—are
allowed to have child
nodes. Since all the different node types are nodes, they inherit the following methods,
even if the use of those methods doesn’t make sense. Using
a method that would affect
the children of a node that can’t have children results in an error. Please consult Figures
4.13 through 4.19 for details concerning which nodes can have children and what kind of
children are allowable.