Chapter 3 DynaScript Predefined Objects
The DOMNode
object
has these properties:
DOMNode.attributes
A DOMNamedNodeMap
containing
the attributes of this node. This property is applicable only to nodes
that are elements.
This fragment tests to see if a node is an
element, and assigns the attributes of the element to a DOMNamedNodeMap
named attlist.
if( node.nodeType == 1 ){ // Element
attlist = node.attributes
} else {
document.writeln( "Not an element" );
}
DOMNode.childNodes
A DOMNodeList
object
that contains all the children of this node. If there are no children,
then this is a node list containing zero nodes.
Performance tip
To loop over a childNodes list, it
is more efficient to use the nextSibling
property
than to explicitly use the childNodes
list
of the parent object.
Not all nodes can have children. This table lists the types of children that are valid for each type of node.
This example loops through the childNodes
of
a node.
function ChildNodes( domDoc ){
docElem = domDoc.documentElement;
var node = docElem.firstChild;
do {
document.writeln( node.nodeName );
node = node.nextSibling;
} while ( node != null )
}
DOMNode.firstChild
The DOMNode
object
representing the first child of this node, or null if there are no
children.
This fragment sets the variable node to the first child of the document element.
var docElem = domDoc.documentElement;
var node = docElem.firstChild;
DOMNode.lastChild
The DOMNode
object
representing the last child of this node, or null if there are no
children.
This fragment sets the variable node to the last child of the document element.
var docElem = domDoc.documentElement;
var node = docElem.lastChild;
DOMNode.nextSibling
The DOMNode
object
representing the node immediately following this node, or null if this
is the last node.
This fragment loops over the siblings of an element named node.
do {
document.writeln( node.nodeName );
node = node.nextSibling;
} while ( node != null )
DOMNode.nodeName
The name of this node. The name of the node
depends on the node type. This table lists the contents of the nodeName
property
for each node type.
XML is case sensitive. If you check the name of a node in a comparison, you must ensure the check is case sensitive.
This fragment writes out the name of each of a set of nodes, held in a variable named node.
do {
document.writeln( node.nodeName );
node = node.nextSibling;
} while ( node != null )
"getElementsByTagName method".
DOMNode.nodeType
An integer representing the type of node object. This table lists the nodeType number for each type of node.
This fragment performs different operations based on the type of node. The node is held in a variable named domNode.
switch domNode.nodeType {
case 1:
//element operations
break;
case 2:
//attribute operations
break;
case 3:
//text operations
break;
case 4:
//CDATA section operations
break;
case 5:
//entity reference operations
break;
case 6:
//entity operations
break;
case 7:
// processing instruction operations
break;
case 8:
//comment operations
break;
case 9:
//document operations
break;
case 10:
// document type operations
break;
case 11:
// document fragment operations
break;
case 12:
//notation operations
break;
default:
document.writeln( "Invalid node type: " +
domChild.nodeType );
}
DOMNode.nodeValue
The value of this node. The value depends on the type of node. This table lists the contents of the nodeValue property for each node type.
This fragment writes out the nodeValue for an attribute named Type:
if( domAtt.nodeName == "Type" ){
document.writeln( domAtt.nodeValue );
}
DOMNode.ownerDocument
The DOMDocument
object
associated with this node. For DOMDocument
objects,
this property is null.
This fragment writes out the name of the document element for the DOMDocument object that owns domNode.
var domDoc = domNode.ownerDocument;
document.writeln( domDoc.documentElement.nodeName );
DOMNode.parentNode
The DOMNode
object
representing the parent of this node, or null if
there is no parent. This node objects do not have a parent: DOMDocument
, DOMDocumentFragment
, DOMAttribute,
DOMEntity, DOMDocumentType, and DOMNotation
.
This example writes out the parent of the node held in the variable domNode, if it exists.
if( domNode.parentNode != null ){
document.writeln( domNode.parentNode.nodeName;
}
DOMNode.previousSibling
The DOMNode
object
representing the node immediately preceding this node, or null if
no nodes precede this node.
This function loops backwards through the children of a node.
function BackwardsChildNodes( domNode ){
var domChild = domNode.lastChild;
do {
document.writeln( domChild.nodeName );
domChild = domChild.previousSibling;
} while ( domChild != null )
}
Copyright © 1999 Sybase, Inc. All rights reserved. |
![]() |