Section 25.209. Node: a node in a document tree


25.209. Node: a node in a document tree

DOM Level 1 Core

25.209.1. Subinterfaces

Attr, CDATASection, CharacterData, Comment, Document, DocumentFragment, DocumentType, Element, ProcessingInstruction, Text

25.209.2. Constants

All Node objects implement one of the subinterfaces listed above. Every Node object has a nodeType property that specifies which subinterface it implements. These constants are the legal values for that property; their names are self-explanatory. Note that these are static properties of the Node( ) constructor function; they are not properties of individual Node objects. Also note that they are not supported by Internet Explorer. For compatibilty with IE, you must use numeric literals directly. For example, use 1 instead of Node.ELEMENT_NODE:

 Node.ELEMENT_NODE = 1;                 // Element Node.ATTRIBUTE_NODE = 2;               // Attr Node.TEXT_NODE = 3;                    // Text Node.CDATA_SECTION_NODE = 4;           // CDATASection Node.PROCESSING_INSTRUCTION_NODE = 7;  // ProcessingInstruction Node.COMMENT_NODE = 8;                 // Comment Node.DOCUMENT_NODE = 9;                // Document Node.DOCUMENT_TYPE_NODE = 10;          // DocumentType Node.DOCUMENT_FRAGMENT_NODE = 11;      // DocumentFragment 

25.209.3. Properties


readonly Attr[] attributes

If this is an Element node, the attributes property is a read-only, array-like object of Attr nodes that represent the attributes of that element. Note that this array is "live": any changes to the attributes of this element are immediately visible through it.

Technically, the attributes[] array is a NamedNodeMap object. The NamedNodeMap interface is specified by the Level 1 DOM standard and defines a number of methods for querying, setting, and removing elements. The Element interface defines better methods for setting and querying element attributes, and there are no other uses of NamedNodeMap that are relevant to client-side JavaScript. For these reasons, therefore, NamedNodeMap is not documented in this book. Treat the attributes property as a read-only array of Attr objects, or use the methods defined by Element to query, set, and delete attributes.


readonly Node[] childNodes

Contains the child nodes of the current node. This property should never be null: for nodes with no children, childNodes is an array with length zero. This property is technically a NodeList object, but it behaves just like a read-only array of Node objects. Note that the NodeList object is live: any changes to this element's list of children are immediately visible through the NodeList.


readonly Node firstChild

The first child of this node, or null if the node has no children.


readonly Node lastChild

The last child of this node, or null if the node has no children.


readonly String localName [DOM Level 2]

In XML documents that use namespaces, specifies the local part of the element or attribute name. This property is never used with HTML documents. See also the namespaceURI and prefix properties.


readonly String namespaceURI [DOM Level 2]

In XML documents that use namespaces, specifies the URI of the namespace of an Element or Attribute node. This property is never used with HTML documents. See also the localName and prefix properties.


readonly Node nextSibling

The sibling node that immediately follows this one in the childNodes[] array of the parentNode, or null if there is no such node.


readonly String nodeName

The name of the node. For Element nodes, specifies the tag name of the element, which can also be retrieved with the tagName property of the Element interface. For other types of nodes, the value depends on the node type. See the upcoming table in the Description section for details.


readonly unsigned short nodeType

The type of the nodei.e., which subinterface the node implements. The legal values are defined by the previously listed constants. Since these constants are not supported by Internet Explorer, however, you may prefer to use hardcoded values instead of the constants. In HTML documents, the common values for this property are 1 for Element nodes, 3 for Text nodes, 8 for Comment nodes, and 9 for the single top-level Document node.


String nodeValue

The value of a node. For Text nodes, it holds the text content. For other node types, the value depends on the nodeType, as shown in the upcoming table in the Description section.


readonly Document ownerDocument

The Document object of which this node is a part. For Document nodes, this property is null.


readonly Node parentNode

The parent (or container) node of this node, or null if there is no parent. Note that the Document, DocumentFragment, and Attr nodes never have parent nodes. Also, nodes that have been removed from the document, or that are newly created and have not yet been inserted into the document tree, have a parentNode of null.


String prefix [DOM Level 2]

For XML documents that use namespaces, specifies the namespace prefix of an Element or Attribute node. This property is never used with HTML documents. See also the localName and namespaceURL properties.


readonly Node previousSibling

The sibling node that immediately precedes this one in the childNodes[] array of the parentNode, or null if there is no such node.


readonly String xml [IE only]

If the node is an XML Document or an Element within an XML document, this IE-specific property returns the text of the element or document as a string. Compare this property to the innerHTML property of HTMLElement, and see XMLSerializer for a cross-platform alternative.

25.209.4. Methods


appendChild( )

Adds a node to the document tree by appending it to the childNodes[] array of this node. If the node is already in the document tree, it is removed and then reinserted at its new position.


cloneNode( )

Makes a copy of this node, or of the node and all its descendants.


hasAttributes( ) [DOM Level 2]

Returns true if this node is an Element and has any attributes.


hasChildNodes( )

Returns true if this node has any children.


insertBefore( )

Inserts a node into the document tree immediately before the specified child of this node. If the node being inserted is already in the tree, it is removed and reinserted at its new location.


isSupported( ) [DOM Level 2]

Returns TRue if the specified version number of a named feature is supported by this node.


normalize( )

"Normalizes" all Text node descendants of this node by deleting empty Text nodes and merging adjacent Text nodes.


removeChild( )

Removes (and returns) the specified child node from the document tree.


replaceChild( )

Removes (and returns) the specified child node from the document tree, replacing it with another node.


selectNodes( ) [IE only]

This IE-specific method performs an XPath query using this node as the root and returns the result as a NodeList. See Document.evaluate( ) and Document.createExpression( ) for DOM-based alternatives.


selectSingleNode( ) [IE only]

This IE-specific method performs an XPath query using this node as the root and returns the result as a single node. See Document.evaluate( ) and Document.createExpression( ) for DOM-based alternatives.


transformNode( ) [IE only]

This IE-specific method applies an XSLT stylesheet to this node and returns the results as a String. See XSLTProcessor for a non-IE alternative.


transformNodeToObject( ) [IE only]

This IE-specific method applies an XSLT stylesheet to this node and returns the results as a new Document object. See XSLTProcessor for a non-IE alternative.

25.209.5. Description

All objects in a document tree (including the Document object itself) implement the Node interface, which provides the fundamental properties and methods for traversing and manipulating the tree. (In Internet Explorer, the Node interface also defines some IE-specific properties and methods for working with XML documents, XPath expressions, and XSLT transforms. See Chapter 21 for details.)

The parentNode property and childNodes[] array allow you to move up and down the document tree. You can enumerate the children of a given node by looping through the elements of childNodes[] or by using the firstChild and nextSibling properties (or the lastChild and previousSibling properties, to loop backward). The appendChild( ), insertBefore( ), removeChild( ), and replaceChild( ) methods allow you to modify the document tree by altering the children of a node.

Every object in a document tree implements both the Node interface and a more specialized subinterface, such as Element or Text. The nodeType property specifies which subinterface a node implements. You can use this property to test the type of a node before using properties or methods of the more specialized interface. For example:

 var n;   // Holds the node we're working with if (n.nodeType == 1) {       // Or use the constant Node.ELEMENT_NODE     var tagname = n.tagName; // If the node is an Element, this is the tag name } 

The nodeName and nodeValue properties specify additional information about a node, but their value depends on nodeType, as shown in the following table. Note that subinterfaces typically define specialized properties (such as the tagName property of Element nodes and the data property of Text nodes) for obtaining this information:

nodeType

nodeName

nodeValue

ELEMENT_NODE

The element's tag name

null

ATTRIBUTE_NODE

The attribute name

The attribute value

TEXT_NODE

#text

The text of the node

CDATA_SECTION_NODE

#cdata-section

The text of the node

PROCESSING_INSTRUCTION_NODE

The target of the PI

The remainder of the PI

COMMENT_NODE

#comment

The text of the comment

DOCUMENT_NODE

#document

null

DOCUMENT_TYPE_NODE

The document type name

null

DOCUMENT_FRAGMENT_NODE

#document-fragment

null


25.209.6. See Also

Document, Element, Text, XMLSerializer, XPathExpression, XSLTProcessor; Chapter 15




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net