HTML DOM Element nodeType-Eigenschaft
- Vorherige Seite nodeName
- Nächste Seite nodeValue
- Nach oben HTML DOM Elements-Objekt
Definition und Verwendung
nodeType
Die Eigenschaft gibt den Knotentyp in numerischer Form zurück.
- Wenn der Knoten ein Elementknoten ist, gibt die Eigenschaft nodeType
1
. - Wenn der Knoten ein Attributknoten ist, gibt die Eigenschaft nodeType
2
. - Wenn der Knoten ein Textknoten ist, gibt die Eigenschaft nodeType
3
. - Wenn der Knoten ein Kommentar-Knoten ist, gibt die Eigenschaft nodeType
8
.
Diese Eigenschaft ist schreibgeschützt.
Weitere Informationen:
Beispiel
Beispiel 1
Erhalten Sie den Knotentyp des <body>-Elements:
var x = document.getElementById("myP").nodeType;
Beispiel 2
Gibt den Knotentyp des <body>-Elements zurück:
document.body.nodeType;
Beispiel 3
Zeigen Sie den Knotentyp aller Elemente an:
const nodes = document.body.childNodes; let text = ""; for (let i = 0; i < nodes.length; i++) { text += nodes[i].nodeType + "<br>"; }
Beispiel 4
Erhalten Sie den Knotennamen, den Wert und den Typ des ersten Kindknotens von "myDIV":
const x = document.getElementById("myDIV").firstChild; let text = ""; text += "Name: " + x.nodeName + "<br>"; text += "Value: " + x.nodeValue + "<br>"; text += "Type: " + x.nodeType;
语法
node.nodeType
返回值
类型 | 描述 |
---|---|
数值 | 节点的节点类型。请见下表。 |
节点类型
HTML 或 XML 文档的文档、元素、属性以及其他节点拥有不同的节点类型。
有 12 种不同的节点类型,它们可能有各种节点类型的子节点:
类型 | 描述 | 子节点 | |
---|---|---|---|
1 | Element | 表示元素 |
|
2 | Attr | 表示属性 |
|
3 | Text | 表示元素或属性中的文本内容 | 无。 |
4 | CDATASection | 表示文档中的 CDATA 部分 (不会被解析器解析的文本) |
无。 |
5 | EntityReference | 表示实体引用 |
|
6 | Entity | 表示实体 |
|
7 | ProcessingInstruction | 表示处理指令 | 无。 |
8 | Comment | 表示注释 | 无。 |
9 | Document | 表示整个文档(DOM 树的根节点) |
|
10 | DocumentType | 向为文档定义的实体提供接口 | 无。 |
11 | DocumentFragment | 表示“轻量级”的 Document 对象,它可保存文档的片段。 |
|
12 | Notation | 表示在 DTD 中声明的符号 | 无。 |
节点类型 - 返回值
每种节点类型的 nodeName 和 nodeValue 属性的返回值:
类型 | nodeName | nodeValue | |
---|---|---|---|
1 | Element | 元素名 | null |
2 | Attr | 属性名 | 属性值 |
3 | Text | #text | 节点的内容 |
4 | CDATASection | #cdata-section | 节点的内容 |
5 | EntityReference | 实体引用的名称 | null |
6 | Entity | 实体名称 | null |
7 | ProcessingInstruction | target | 节点的内容 |
8 | Comment | #comment | 注释文本 |
9 | Document | #document | null |
10 | DocumentType | doctype 名称 | null |
11 | DocumentFragment | #document 片段 | null |
12 | Notation | 符号名称 | null |
节点类型 - 命名常量
类型 | 命名常量 |
---|---|
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.nodeType
是 DOM Level 1 (1998) 特性。
所有浏览器都完全支持它:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Unterstützung | 9-11 | Unterstützung | Unterstützung | Unterstützung | Unterstützung |
- Vorherige Seite nodeName
- Nächste Seite nodeValue
- Nach oben HTML DOM Elements-Objekt