得到 body 元素的節點類型:node
document.body.nodeType;
結果:瀏覽器
1
nodeType 屬性返回以數字值返回指定節點的節點類型。spa
若是節點是元素節點,則 nodeType 屬性將返回 1。對象
若是節點是屬性節點,則 nodeType 屬性將返回 2。接口
IE | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|
√ | √ | √ | √ | √ |
全部主流瀏覽器均支持 nodeType 屬性。文檔
node.nodeType
返回值: | 字符串值,表明節點的節點類型。 |
DOM 版本 | Core Level 1 Node Object |
文檔、元素、屬性以及 HTML 或 XML 文檔的其餘方面擁有不一樣的節點類型。字符串
存在 12 種不一樣的節點類型,其中可能會有不一樣節點類型的子節點:get
節點類型 | 描述 | 子節點 | |
---|---|---|---|
1 | Element | 表明元素 | Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference |
2 | Attr | 表明屬性 | Text, EntityReference |
3 | Text | 表明元素或屬性中的文本內容。 | 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 樹的根節點)。 | Element, ProcessingInstruction, Comment, DocumentType |
10 | DocumentType | 向爲文檔定義的實體提供接口 | None |
11 | DocumentFragment | 表明輕量級的 Document 對象,可以容納文檔的某個部分 | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
12 | Notation | 表明 DTD 中聲明的符號。 | None |
對於每種節點類型,nodeName 和 nodeValue 屬性的返回值:it
節點類型 | nodeName 返回 | nodeValue 返回 | |
---|---|---|---|
1 | Element | 元素名 | null |
2 | Attr | 屬性名稱 | 屬性值 |
3 | Text | #text | 節點的內容 |
4 | CDATASection | #cdata-section | 節點的內容 |
5 | EntityReference | 實體引用名稱 | null |
6 | Entity | 實體名稱 | null |
7 | ProcessingInstruction | target | 節點的內容 |
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 |