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;Testen Sie heraus‹/›
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:
Knotenart | Description | Child node | |
---|---|---|---|
1 | Element | Represents an element | Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference |
2 | Attr | Represents an attribute | Text, EntityReference |
3 | Text | Represents the 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 an entity reference. | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
6 | Entity | Represents an entity. | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
7 | ProcessingInstruction | Represents processing instructions. | None |
8 | Comment | stellt die Kommentare dar. | None |
9 | Document | stellt das gesamte Dokument (Wurzelknoten des DOM-Baums) dar. | Element, ProcessingInstruction, Comment, DocumentType |
10 | DocumentType | bietet eine Schnittstelle für von Dokumenten definierte Entitäten | None |
11 | DocumentFragment | stellt ein leichten Document-Objekt dar, das einen Teil des Dokuments enthalten kann | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
12 | Notation | stellt die in DTD deklarierten Symbole dar. | None |
Für jeden Knotenart gibt die nodename- und nodeValue-Attribute zurück:
Knotenart | nodeName wird zurückgegeben | nodeValue wird zurückgegeben | |
---|---|---|---|
1 | Element | Elementname | null |
2 | Attr | Attributname | Attributwert |
3 | Text | #text | Inhalt des Knotens |
4 | CDATASection | #cdata-Abschnitt | Inhalt des Knotens |
5 | EntityReference | Entitätsbezeichner | null |
6 | Entity | Entitätname | null |
7 | ProcessingInstruction | target | Inhalt des Knotens |
8 | Comment | #comment | Kommentartext |
9 | Document | #document | null |
10 | DocumentType | Dokumenttypname | null |
11 | DocumentFragment | #document Fragment | null |
12 | Notation | Symbolname | null |
Knotenart | benannt als Konstante |
---|---|
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 |
Dieser Beispiel überprüft, ob der erste Knoten im document-Element ein Kommentar-Knoten ist. Wenn nicht, wird eine Nachricht angezeigt:
var node = document.documentElement.firstChild; if (node.nodeType != Node.COMMENT_NODE) { alert("Sie sollten Ihren Code gut kommentieren!"); }Testen Sie heraus‹/›
Geben Sie den Node-Namen, den Node-Typ und den Node-Wert des ersten Kindknotens des div zurück:
<div id="div-1">Dies ist ein div-Element.</div> <script> var x = document.getElementById("div-1).firstChild; var txt = ""; txt += "Der Node-Name: " + x.nodeName + "<br>"; txt += "Der Node-Wert: " + x.nodeValue + "<br>"; txt += "Der Node-Typ: " + x.nodeType; document.getElementById("para").innerHTML = txt; </script>Testen Sie heraus‹/›
HTML DOM-Referenz:node .nodeName-Eigenschaft
HTML DOM-Referenz:node .nodeValue-Eigenschaft
HTML DOM-Referenz:node .childNodes-Eigenschaft