Loại nút phần tử DOM HTML
Ví dụ
Lấy loại nút của phần tử cơ thể:
var x = document.getElementById("myP").nodeType;
Hãy tự mình thử »Thêm ví dụ "Hãy tự mình thử" bên dưới.
Sự miêu tả
Thuộc tính nodeType trả về loại nút, dưới dạng số, của nút đã chỉ định.
Nếu nút là nút phần tử, thuộc tính nodeType sẽ trả về 1.
Nếu nút là nút thuộc tính thì thuộc tính nodeType sẽ trả về 2.
Nếu nút là nút văn bản, thuộc tính nodeType sẽ trả về 3.
Nếu nút là nút nhận xét, thuộc tính nodeType sẽ trả về 8.
Thuộc tính này là chỉ đọc.
Cú pháp
node .nodeType
Giá trị trả về
Kiểu | Sự miêu tả |
Con số | Loại nút của nút. Xem bảng dưới đây. |
Các loại nút
Tài liệu, thành phần, thuộc tính và các nút khác của tài liệu HTML hoặc XML có các loại nút khác nhau.
Có 12 loại nút khác nhau, có thể có con thuộc nhiều loại nút khác nhau:
Type | Description | Children | |
---|---|---|---|
1 | Element | Represents an element | Element, Text, Comment, ProcessingInstruction, CDATASection, EntityReference |
2 | Attr | Represents an attribute | Text, EntityReference |
3 | Text | Represents textual content in an element or attribute | None |
4 | CDATASection | Represents a CDATA section in a document (text that will NOT be parsed by a parser) | None |
5 | EntityReference | Represents an entity reference | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
6 | Entity | Represents an entity | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
7 | ProcessingInstruction | Represents a processing instruction | None |
8 | Comment | Represents a comment | None |
9 | Document | Represents the entire document (the root-node of the DOM tree) | Element, ProcessingInstruction, Comment, DocumentType |
10 | DocumentType | Provides an interface to the entities defined for the document | None |
11 | DocumentFragment | Represents a "lightweight" Document object, which can hold a portion of a document | Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference |
12 | Notation | Represents a notation declared in the DTD | None |
Các loại nút - Giá trị trả về
Giá trị trả về của thuộc tính nodeName và nodeValue cho từng loại nút:
Type | nodeName | nodeValue | |
---|---|---|---|
1 | Element | element name | null |
2 | Attr | attribute name | attribute value |
3 | Text | #text | content of node |
4 | CDATASection | #cdata-section | content of node |
5 | EntityReference | entity reference name | null |
6 | Entity | entity name | null |
7 | ProcessingInstruction | target | content of node |
8 | Comment | #comment | comment text |
9 | Document | #document | null |
10 | DocumentType | doctype name | null |
11 | DocumentFragment | #document fragment | null |
12 | Notation | notation name | null |
NodeTypes - Hằng số được đặt tên
Type | 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 |
Thêm ví dụ
Ví dụ
Hiển thị các loại nút của tất cả các phần tử:
const nodes = document.body.childNodes;
let text = "";
for (let i = 0; i < nodes.length; i++) {
text += nodes[i].nodeType + "<br>";
}
Hãy tự mình thử »Ví dụ
Lấy tên nút, giá trị và loại nút con đầu tiên của "myDIV":
const x = document.getElementById("myDIV").firstChild;
let text = "";
text += "Name: " + x.nodeName + "<br>";
text += "Value: " + x.nodeValue + "<br>";
text += "Type: " + x.nodeType;
Hãy tự mình thử »Hỗ trợ trình duyệt
element.nodeType
là tính năng DOM Cấp 1 (1998).
Nó được hỗ trợ đầy đủ trong tất cả các trình duyệt:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |