English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM nodeType attribute

HTML DOM Element-Objekt

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.

Syntax:

node.nodeType
var x = document.getElementById("myPara").nodeType;
Testen Sie heraus‹/›

Browser compatibility

All browsers fully support the nodeType attribute:

Attribute
nodeTypeIsIsIsIsIs

Technical details

Return value:A number representing the node type of the node
DOM version:DOM level1

Node Types (Node Types)

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:

KnotenartDescriptionChild node
1ElementRepresents an elementElement, Text, Comment, ProcessingInstruction, CDATASection, EntityReference
2AttrRepresents an attributeText, EntityReference
3TextRepresents the text content within an element or attribute.None
4CDATASectionRepresents the CDATA section in a document (text that will not be parsed by the parser).None
5EntityReferenceRepresents an entity reference.Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
6EntityRepresents an entity.Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
7ProcessingInstructionRepresents processing instructions.None
8Commentstellt die Kommentare dar.None
9Documentstellt das gesamte Dokument (Wurzelknoten des DOM-Baums) dar.Element, ProcessingInstruction, Comment, DocumentType
10DocumentTypebietet eine Schnittstelle für von Dokumenten definierte EntitätenNone
11DocumentFragmentstellt ein leichten Document-Objekt dar, das einen Teil des Dokuments enthalten kannElement, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
12Notationstellt die in DTD deklarierten Symbole dar.None

Knotenart - Rückgabewert

Für jeden Knotenart gibt die nodename- und nodeValue-Attribute zurück:

KnotenartnodeName wird zurückgegebennodeValue wird zurückgegeben
1ElementElementnamenull
2AttrAttributnameAttributwert
3Text#textInhalt des Knotens
4CDATASection#cdata-AbschnittInhalt des Knotens
5EntityReferenceEntitätsbezeichnernull
6EntityEntitätnamenull
7ProcessingInstructiontargetInhalt des Knotens
8Comment#commentKommentartext
9Document#documentnull
10DocumentTypeDokumenttypnamenull
11DocumentFragment#document Fragmentnull
12NotationSymbolnamenull

Knotenart-Benennungskonstante

Knotenartbenannt als Konstante
1ELEMENT_NODE
2ATTRIBUTE_NODE
3TEXT_NODE
4CDATA_SECTION_NODE
5ENTITY_REFERENCE_NODE
6ENTITY_NODE
7PROCESSING_INSTRUCTION_NODE
8COMMENT_NODE
9DOCUMENT_NODE
10DOCUMENT_TYPE_NODE
11DOCUMENT_FRAGMENT_NODE
12NOTATION_NODE

Mehr Beispiele

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‹/›

Zusammenhängende Referenzen

HTML DOM-Referenz:node .nodeName-Eigenschaft

HTML DOM-Referenz:node .nodeValue-Eigenschaft

HTML DOM-Referenz:node .childNodes-Eigenschaft

HTML DOM Element-Objekt