Chapter 3 DynaScript Predefined Objects
The DOMNode
object
has these methods:
DOMNode.appendChild( newChild )
Adds the DOMNode
newChild to
the end of the list of children of this node. If newChild already exists
in the tree, it is first removed.
The DOMNode
which
was added or null if the node could not be appended (for example,
if newChild is not a valid type of child for
this node).
This fragment adds a child element to a node held in the customer variable.
new_elem = domDoc.createElement( "Name" );
new_text = domDoc.createTextNode("Ann T. Dote" );
el = customer.appendChild( new_elem );
el.appendChild( new_text );
DOMNode.cloneNode( deep )
Returns a duplicate of this DOMNode
.
The duplicate node has no parent. deep is a
Boolean indicating whether to clone the subtree under this node.
For DOMElement
objects,
all attributes and their values are copied.
The duplicate DOMNode
.
This fragment makes a duplicate of the first
child of a document element (and its subtree) and adds it to the
end of the list of its children. The call to prettyPrint
is
for debugging purposes.
var docElem = domDoc.documentElement;
var thisNode = docElem.firstChild;
var newNode = thisNode.cloneNode( true );
docElem.appendChild( newNode );
document.writeln( domDoc.prettyPrint() );
DOMNode.hasChildNodes( )
Indicates whether a node has any children
Boolean.
This statement tests to see whether thisNode
has
any children.
if( thisNode.hasChildNodes() ){
// operations here
}
DOMNode.insertBefore( newChild, refChild )
Inserts the DOMNode
newChild before
the existing child DOMNode
refChild.
If refChild is null, newChild is
inserted at the end of the list of children.
If newChild is a DOMDocumentFragment
,
all of its children are inserted, in order, before refChild.
If newChild already exists in the tree, it
is first removed.
The inserted DOMNode
,
or null if the node could not be inserted (for example, if refChild is
not a child of this node).
This fragment makes a duplicate of the first
child of a document element (and its subtree) and adds it as the
penultimate child. The call to prettyPrint
is for
debugging purposes.
var docElem = domDoc.documentElement;
var thisNode = docElem.firstChild;
var newNode = thisNode.cloneNode( true );
docElem.insertBefore( newNode, docElem.lastChild );
document.writeln( domDoc.prettyPrint() );
DOMNode.removeChild( oldChild )
Removes from the list of children, the child node indicated by oldChild.
The removed DOMNode
or
null if the child could not be removed (for example, if oldChild is
not a child of this node.
This fragment removes the last child of the document element from a document.
var docElem = domDoc.documentElement;
var thisNode = docElem.lastChild;
var oldNode = docElem.removeChild( thisNode );
DOMNode.replaceChild( newChild, oldChild )
Replaces the child DOMNode
oldChild with
the DOMNode
newChild in
the list of children. If newChild already exists
in the tree, it is first removed.
The replaced DOMNode
(oldChild)
or null if the node could not be replaced (for example, if oldChild is
not a child of this node).
This fragment replaces the last child of the document element with the first child. The first child is removed from its place in the beginning of the list.
var thisNode = docElem.firstChild;
var oldNode = docElem.replaceChild( thisNode,
docElem.lastChild );
Copyright © 1999 Sybase, Inc. All rights reserved. |