下面有更多 "亲自试一试" 示例。
nodeType 属性以数字形式返回指定节点的节点类型。
如果该节点是元素节点,则 nodeType 属性将返回 1。
如果节点是属性节点,则nodeType属性将返回2。
如果节点是文本节点,则 nodeType 属性将返回 3。
如果该节点是注释节点,则nodeType属性将返回8。
该属性是只读的。
node.nodeType
类型 | 描述 |
数字 | 节点的节点类型。 见下表。 |
HTML 或 XML 文档的文档、元素、属性和其他节点具有不同的节点类型。
有 12 种不同的节点类型,它们可能有各种节点类型的子节点:
Type | Description | Children | |
---|---|---|---|
1 | Element | Represents an element | Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference |
2 | Attr | Represents an attribute | Text, EntityReference |
3 | Text | Represents textual content in an element or attribute | None |
4 | CDATASection | Represents a CDATA section in a document (text that will NOT be parsed by a 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 a processing instruction | None |
8 | Comment | Represents a comment | None |
9 | Document | Represents the entire document (the root-node of the DOM tree) | Element, ProcessingInstruction, Comment, DocumentType |
10 | DocumentType | Provides an interface to the entities defined for the document | None |
11 | DocumentFragment | Represents a "lightweight" Document object, which can hold a portion of a document | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
12 | Notation | Represents a notation declared in the DTD | None |
每个节点类型的nodeName 和nodeValue 属性的返回值:
Type | nodeName | nodeValue | |
---|---|---|---|
1 | Element | element name | null |
2 | Attr | attribute name | attribute value |
3 | Text | #text | content of node |
4 | CDATASection | #cdata-section | content of node |
5 | EntityReference | entity reference name | null |
6 | Entity | entity name | null |
7 | ProcessingInstruction | target | content of node |
8 | Comment | #comment | comment text |
9 | Document | #document | null |
10 | DocumentType | doctype name | null |
11 | DocumentFragment | #document fragment | null |
12 | Notation | notation name | null |
Type | 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 |
显示所有元素的节点类型:
const nodes = document.body.childNodes;
let text = "";
for (let i = 0; i < nodes.length; i++) {
text += nodes[i].nodeType + "<br>";
}
亲自试一试 »
获取"myDIV"s第一个子节点的节点名称、值和类型:
const x = document.getElementById("myDIV").firstChild;
let text = "";
text += "Name: " + x.nodeName + "<br>";
text += "Value: " + x.nodeValue + "<br>";
text += "Type: " + x.nodeType;
亲自试一试 »
element.nodeType
是 DOM Level 1 (1998) 功能。
所有浏览器都完全支持它:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!