238Part IIIDocument Objects Reference} } } A more (Web hosting directory)

238Part IIIDocument Objects Reference} } } A more efficient approach uses the getElementsByTagName()method to retrieve a collec- tion of all pelements and then iterate through them directly. Related Item:tagNameproperty. nodeTypeValue:Integer.Read-Only Compatibility:WinIE5+, MacIE5+, NN6+, Moz1+, Safari1+ The W3C DOM specification identifies a series of constant values that denote categories ofnodes. Every node has a value that identifies its type, but not all browsers support thenodeTypeproperty on all node types as objects. Table 15-4 lists the nodeTypevalues imple- mented in recent browsers; all of the values are considered part of the W3C DOM Level 2specification. Table 15-4: nodeType Property ValuesValueDescriptionWinIEMacIENN/MozSafari1Element node55NN612Attribute node65NN613Text (#text) node55NN614CDATA section node—- 5Entity reference node—- 6Entity node—- 7Processing instruction node—- 8Comment node65NN6- 9Document node55NN6110Document type node–NN6111Document fragment node65NN6112Notation node—- The nodeTypevalue is automatically assigned to a node, whether the node exists in the docu- ment s HTML source code or it is generated on the fly via a script. For example, if you createa new element node through any of the ways available by script (for example, by assigning astring encased in HTML tags to the innerHTMLproperty or by explicitly invoking the document. createElement()method), the new element assumes a nodeTypeof 1. Mozilla-based browsers and Safari go one step further in supporting the W3C DOM specificationby implementing a set of Nodeobject property constants for each of the nodeTypevalues. Table 15-5 lists the entire set as defined in the DOM Level 2 specification. Substituting theseconstants for nodeTypeintegers can improve readability of a script. For example, instead ofif (myElem.nodeType == 1) {…} it is much easier to see what s going on withif (myElem.nodeType == Node.ELEMENT_NODE) {…} elementObject.nodeName

Leave a Reply