第1章 第375节 dom element nodeType

推荐给朋友

HTML DOM nodeType 属性

实例

返回body元素的节点类型:

document.body.nodeType;

输出结果:

1

尝试一下

定义和用法

nodeType 属性返回节点类型。

如果节点是一个元素节点,nodeType 属性返回 1。

如果节点是属性节点, nodeType 属性返回 2。

如果节点是一个文本节点,nodeType 属性返回 3。

如果节点是一个注释节点,nodeType 属性返回 8。

该属性是只读的。

浏览器支持

所有主要浏览器都支持 nodeType 属性。

语法

node.nodeType

技术细节

返回值: 字符串, 代表节点的节点类型。
DOM 版本 Core Level 1 Node Object

节点类型

一个HTML或XML文档的文件,元素,属性等有不同的节点类型。

有12种不同的节点类型,不同的节点类型也可以有不同的子节点类型:

节点类型 描述 子节点
1 Element 一个元素 Element, Text, Comment,
ProcessingInstruction, CDATASection,
Entity参考手册
2 Attr 一个属性 Text, Entity参考手册
3 Text 一个元素的文本内容 或属性 None
4 CDATASection 一个文档的CDATA部分
(文本将 不会被解析器解析)
None
5 Entity参考手册 实体引用 Element, ProcessingInstruction,
Comment, Text, CDATASection,
Entity参考手册
6 Entity 一个实体 Element, ProcessingInstruction,
Comment, Text, CDATASection,
Entity参考手册
7 ProcessingInstruction 一个处理指令 None
8 Comment 一个注释 None
9 Document 整个文档(DOM树的根节点) Element, ProcessingInstruction,
Comment, DocumentType
10 DocumentType 为文档实体提供接口 None
11 DocumentFragment 表示邻接节点和它们的子树。 Element, ProcessingInstruction,
Comment, Text, CDATASection,
Entity参考手册
12 Notation 代表一个符号在DTD中的声明 None

节点类型 - 返回值

返回节点名和每个节点类型节点值属性的值:

节点类型 nodeName 返回值 nodeValue 返回值
1 Element 元素名 null
2 Attr 属性名 属性值
3 Text #text 节点内容
4 CDATASection #cdata-section 节点内容
5 Entity 参考手册 实体参考名 null
6 Entity 实体名 null
7 ProcessingInstruction target 节点的内容
8 Comment #comment 注释文本
9 Document #document null
10 DocumentType doctype name null
11 DocumentFragment #document fragment null
12 Notation 符号名称 null

节点类型 - 静态变量名

Node类型 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