HTML DOM nodeTypeProperty

元素对象参考手册Element Object

Examples

Returns the node type of the body element:

document.body.nodeType;

Output:

1

Try it yourself »

Definition and Usage

The nodeType property returns the node type.

If the node is an element node, the nodeType property returns 1.

If the node is an attribute node, the nodeType property returns 2.

If the node is a text node, the nodeType property returns 3.

If the node is a comment node, the nodeType property returns 8.

This property is read-only.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the nodeType property


Syntax

node.nodeType

Technical Details

Return Value: String, representing the node type of the node.
DOM Version Core Level 1 Node Object


Node Types

An HTML or XML document, its elements, attributes, etc. have different node types.

There are 12 different node types, and different node types can also have different child node types:

Node Type Description Child Nodes
1 Element An element Element, Text, Comment, ProcessingInstruction, CDATASection, Entity Reference
2 Attr An attribute Text, Entity Reference
3 Text The text content of an element or attribute None
4 CDATASection A CDATA section of a document (text will not be parsed by the parser) None
5 Entity Reference Entity Reference Element, ProcessingInstruction, Comment, Text, CDATASection, Entity Reference
6 Entity An entity Element, ProcessingInstruction, Comment, Text, CDATASection, Entity Reference
7 ProcessingInstruction A processing instruction None
8 Comment A comment None
9 Document The entire document (the root node of the DOM tree) Element, ProcessingInstruction, Comment, DocumentType
10 DocumentType Provides an interface for document entities None
11 DocumentFragment Represents adjacent nodes and their subtrees. Element, ProcessingInstruction, Comment, Text, CDATASection, Entity Reference
12 Notation Represents a declaration of a notation in the DTD None

Node Types - Return Values

Returns the node name and the value of the node value property for each node type:

Node Type nodeName Return Value nodeValue Return Value
1 Element Element name null
2 Attr Attribute name Attribute value
3 Text #text Node content
4 CDATASection #cdata-section Node content
5 Entity Reference Entity reference name null
6 Entity Entity name null
7 ProcessingInstruction target Node content
8 Comment #comment Comment text
9 Document #document null
10 DocumentType doctype name null
11  DocumentFragment #document fragment null
12 Notation Notation name null

Node Types - Static Variable Names

Node Types Named Constant
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
3 TEXT_NODE
4 CDATA_SECTION_NODE
5 ENTITY_REFERENCE_NODE
6 ENTITY_NODE
7 PROCESSING_INSTRUCTION_NODE
8 COMMENT_NODE
9 DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE


元素对象参考手册Element Object

Other Extensions