nodeType 屬性返回以數(shù)字值返回指定節(jié)點(diǎn)的節(jié)點(diǎn)類型。
如果節(jié)點(diǎn)是元素節(jié)點(diǎn),則 nodeType 屬性將返回 1。
如果節(jié)點(diǎn)是屬性節(jié)點(diǎn),則 nodeType 屬性將返回 2。
IE | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|
所有主流瀏覽器均支持 nodeType 屬性。
node.nodeType
返回值: | 字符串值,代表節(jié)點(diǎn)的節(jié)點(diǎn)類型。 |
DOM 版本 | Core Level 1 Node Object |
文檔、元素、屬性以及 HTML 或 XML 文檔的其他方面擁有不同的節(jié)點(diǎn)類型。
存在 12 種不同的節(jié)點(diǎn)類型,其中可能會有不同節(jié)點(diǎn)類型的子節(jié)點(diǎn):
節(jié)點(diǎn)類型 | 描述 | 子節(jié)點(diǎn) | |
---|---|---|---|
1 | Element | 代表元素 | Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference |
2 | Attr | 代表屬性 | Text, EntityReference |
3 | Text | 代表元素或?qū)傩灾械奈谋緝?nèi)容。 | None |
4 | CDATASection | 代表文檔中的 CDATA 部分(不會由解析器解析的文本)。 | None |
5 | EntityReference | 代表實體引用。 | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
6 | Entity | 代表實體。 | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
7 | ProcessingInstruction | 代表處理指令。 | None |
8 | Comment | 代表注釋。 | None |
9 | Document | 代表整個文檔(DOM 樹的根節(jié)點(diǎn))。 | Element, ProcessingInstruction, Comment, DocumentType |
10 | DocumentType | 向為文檔定義的實體提供接口 | None |
11 | DocumentFragment | 代表輕量級的 Document 對象,能夠容納文檔的某個部分 | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
12 | Notation | 代表 DTD 中聲明的符號。 | None |
對于每種節(jié)點(diǎn)類型,nodeName 和 nodeValue 屬性的返回值:
節(jié)點(diǎn)類型 | nodeName 返回 | nodeValue 返回 | |
---|---|---|---|
1 | Element | 元素名 | null |
2 | Attr | 屬性名稱 | 屬性值 |
3 | Text | #text | 節(jié)點(diǎn)的內(nèi)容 |
4 | CDATASection | #cdata-section | 節(jié)點(diǎn)的內(nèi)容 |
5 | EntityReference | 實體引用名稱 | null |
6 | Entity | 實體名稱 | null |
7 | ProcessingInstruction | target | 節(jié)點(diǎn)的內(nèi)容 |
8 | Comment | #comment | 注釋文本 |
9 | Document | #document | null |
10 | DocumentType | 文檔類型名稱 | null |
11 | DocumentFragment | #document 片段 | null |
12 | Notation | 符號名稱 | null |
NodeType | 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 |