English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The nodeType read-only property returns the node type of the specified node in numeric form.
The nodeType attribute can be used to distinguish between different types of nodes, such as elements, text, and comments.
If the node is an element node, the nodeType attribute will return1.
If the node is an attribute node, the nodeType attribute will return2.
If the node is a text node, the nodeType attribute will return3.
If the node is a comment node, the nodeType attribute will return8.
node.nodeType
var x = document.getElementById("myPara").nodeType;Test and See‹/›
All browsers fully support the nodeType attribute:
Attribute | |||||
nodeType | Is | Is | Is | Is | Is |
Return value: | A number representing the node type of the node |
---|---|
DOM Version: | DOM Level1 |
Documents, elements, attributes, and other aspects of HTML or XML documents have different node types.
Exists 12 There are different types of nodes, some of which may have child nodes of different types:
Node type | Description | Child nodes | |
---|---|---|---|
1 | Element | Represents elements | Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference |
2 | Attr | Represents attributes | Text, EntityReference |
3 | Text | Represents text content within an element or attribute. | None |
4 | CDATASection | Represents the CDATA section in a document (text that will not be parsed by the parser). | None |
5 | EntityReference | Represents entity references. | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
6 | Entity | Represents entities. | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
7 | ProcessingInstruction | Represents processing instructions. | None |
8 | Comment | Represents comments. | None |
9 | Document | Represents the entire document (the root node of the DOM tree). | Element, ProcessingInstruction, Comment, DocumentType |
10 | DocumentType | Provides an interface to entities defined for the document | None |
11 | DocumentFragment | Represents a lightweight Document object that can contain a part of the document | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
12 | Notation | Represents the symbol declared in DTD. | None |
For each node type, the return values of nodeName and nodeValue properties are:
Node type | nodeName returns | nodeValue returns | |
---|---|---|---|
1 | Element | Element name | null |
2 | Attr | Attribute name | Attribute value |
3 | Text | #text | The content of the node |
4 | CDATASection | #cdata-section | The content of the node |
5 | EntityReference | Entity reference name | null |
6 | Entity | Entity name | null |
7 | ProcessingInstruction | target | The content of the node |
8 | Comment | #comment | Comment text |
9 | Document | #document | null |
10 | DocumentType | Document type name | null |
11 | DocumentFragment | #document fragment | null |
12 | Notation | Symbol name | null |
Node type | Named as a 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 |
This example checks if the first node within the document element is a comment node, and if not, it displays a message:
var node = document.documentElement.firstChild; if (node.nodeType != Node.COMMENT_NODE) { alert("You should comment your code well!"); }Test and See‹/›
Return the node name, node type, and node value of the first child of the div element:
<div id="div-1">This is a div element.</div> <script> var x = document.getElementById("div-1).firstChild; var txt = ""; txt += "The node name: " + x.nodeName + "<br>"; txt += "The node value: " + x.nodeValue + "<br>"; txt += "The node type: " + x.nodeType; document.getElementById("para").innerHTML = txt; </script>Test and See‹/›
HTML DOM Reference:node .nodeName property
HTML DOM Reference:node .nodeValue property
HTML DOM Reference:node .childNodes property