HTML DOM nodeType 속성
예시
body 요소의 노드 유형을 가져옵니다.
var x = document.getElementById("myP").nodeType;
아래에서 더 많은 "직접 사용해 보기" 예를 살펴보세요.
정의 및 사용
nodeType 속성은 지정된 노드의 노드 유형을 숫자로 반환합니다.
노드가 요소 노드인 경우 nodeType 속성은 1을 반환합니다.
노드가 속성 노드인 경우 nodeType 속성은 2를 반환합니다.
노드가 텍스트 노드인 경우 nodeType 속성은 3을 반환합니다.
노드가 주석 노드인 경우 nodeType 속성은 8을 반환합니다.
이 속성은 읽기 전용입니다.
브라우저 지원
Property | |||||
---|---|---|---|---|---|
nodeType | Yes | Yes | Yes | Yes | Yes |
통사론
node.nodeType
기술적 세부 사항
반환 값: | 노드의 노드 유형을 나타내는 숫자 |
---|---|
DOM 버전 | 코어 레벨 1 노드 개체 |
노드 유형
HTML 또는 XML 문서의 문서, 요소, 속성 및 기타 측면에는 서로 다른 노드 유형이 있습니다.
다양한 노드 유형의 자식을 가질 수 있는 12개의 서로 다른 노드 유형이 있습니다.
Node 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 |
노드 유형 - 반환 값
각 노드 유형에 대한 nodeName 및 nodeValue 속성의 반환 값:
Node type | nodeName returns | nodeValue returns | |
---|---|---|---|
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 - 명명된 상수
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 |
더 많은 예
예시
<body> 요소의 노드 유형을 가져옵니다.
var x = document.body.nodeType;
예시
<body> 요소의 자식 노드의 노드 유형을 가져옵니다.
var c = document.body.childNodes;
var txt = "";
var i;
for (i = 0; i < c.length; i++) {
txt = txt + c[i].nodeType + "<br>";
}
document.getElementById("demo").innerHTML = txt;
예시
<div> 요소의 첫 번째 자식 노드의 노드 이름, 노드 값 및 노드 유형을 가져옵니다.
<div id="myDIV">This is a div element.</div>
<script>
var x = document.getElementById("myDIV").firstChild;
var txt = "";
txt += "The node name: " + x.nodeName + "<br>";
txt += "The node value: " + x.nodeValue + "<br>";
txt += "The node type: " + x.nodeType;
</script>
관련 페이지
HTML DOM 참조: 노드. nodeName 속성
HTML DOM 참조: 노드. nodeValue 속성
HTML DOM 참조: 노드. childNodes 속성