只读
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.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
节点。
|
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
.
|
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. |
过时 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
节点
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument