只读 Node.nodeType property is an integer that identifies what the node is. It distinguishes different kind of nodes from each other, such as 元素 , text and comments .

句法

var type = node.nodeType;
					

Returns an integer which specifies the type of the node. Possible values are listed in Node type constants .

常量

Node type constants

常量 描述
Node.ELEMENT_NODE 1 元素 node like <p> or <div> .
Node.TEXT_NODE 3 实际 文本 inside an 元素 or Attr .
Node.CDATA_SECTION_NODE 4 A CDATASection ,譬如 <!CDATA[[ … ]]> .
Node.PROCESSING_INSTRUCTION_NODE 7 A ProcessingInstruction of an XML document, such as <?xml-stylesheet … ?> .
Node.COMMENT_NODE 8 A 注释 node, such as <!-- … --> .
Node.DOCUMENT_NODE 9 A Document 节点。
Node.DOCUMENT_TYPE_NODE 10 A DocumentType node, such as <!DOCTYPE html> .
Node.DOCUMENT_FRAGMENT_NODE 11 A DocumentFragment 节点。

Deprecated node type constants

The following constants have been deprecated and should not be used anymore.

常量 Description
Node.ATTRIBUTE_NODE 2 属性 of an 元素 . Attributes no longer implement the 节点 interface as of DOM4 .
Node.ENTITY_REFERENCE_NODE 5 An XML Entity Reference node, such as &foo; . Removed in DOM4 .
Node.ENTITY_NODE 6 An XML <!ENTITY …> node. Removed in DOM4 .
Node.NOTATION_NODE 12 An XML <!NOTATION …> node. Removed in DOM4 .

范例

Different types of nodes

document.nodeType === Node.DOCUMENT_NODE; // true
document.doctype.nodeType === Node.DOCUMENT_TYPE_NODE; // true
document.createDocumentFragment().nodeType === Node.DOCUMENT_FRAGMENT_NODE; // true
var p = document.createElement("p");
p.textContent = "Once upon a time…";
p.nodeType === Node.ELEMENT_NODE; // true
p.firstChild.nodeType === Node.TEXT_NODE; // true
					

注释

This example checks if the first node inside the document element is a comment, and displays a message if not.

var node = document.documentElement.firstChild;
if (node.nodeType !== Node.COMMENT_NODE) {
  console.warn("You should comment your code!");
}
					

规范

规范 状态 注释
DOM
The definition of 'Node.nodeType' in that specification.
实时标准 弃用 ATTRIBUTE_NODE , ENTITY_REFERENCE_NODE and NOTATION_NODE 类型。
DOM (文档对象模型) 3 级核心规范
The definition of 'Node.nodeType' in that specification.
过时 No changes.
DOM (文档对象模型) 级别 2 核心规范
The definition of 'Node.nodeType' in that specification.
过时 No changes.
DOM (文档对象模型) 1 级规范
The definition of 'Node.nodeType' in that specification.
过时 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
nodeType Chrome 1 Edge 12 Firefox 1 IE 6 Opera 7 Safari 1.1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

元数据

  • 最后修改: