Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- MIPS
- mysql
- Java
- XML
- Pipelining
- Algorithm
- computer
- Class
- DATAPATH
- DB
- DS
- instruction
- github
- for
- python
- MacOS
- control
- function
- data structure
- web
- react
- DoM
- Linux
- architecture
- CSS
- javascript
- php
- while
- system
- html
Archives
- Today
- Total
YYYEJI
[JavaScript] nodeType이란 본문
728x90
↓↓↓ DOM 개념 알아보기 ↓↓↓
https://yyyeji.tistory.com/160
NodeType이란
Element, Attribute, Text . . . 어떤 종류냐에 따라서 특정 number을 가지게 됩니다.
Node Type | NodeType |
Element | 1 |
Attribute | 2 |
Text | 3 |
Comment | 8 |
Document | 9 |
NodeType은 언제 사용하는지 예제를 통해 살펴보겠습니다.
<bookstore>
<book category="cooking">
<title lang ="en"> Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang = 'en'> Harry Potter </title>
<author>J K.Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang = 'en'> XQuery Kick Start </title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang = 'en'> Learning XML </title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
↑↑↑ Books.xml ↑↑↑
function myFunction(xml) {
var x, i, xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.documentElement.childNodes;
for (i = 0; i<x.length; i++){
if(x[i].nodeType == 1){
txt += x[i].nodeName + "<br>";
}
}
document.getElementById("demo").innerHTML = txt;
}
↑↑↑ h.html ↑↑↑
Function의 조건문을 살펴보겠습니다.
if(x[i].nodeType == 1){
→ x[i]의 값이 1인지를 판단하게 됩니다.
→ nodeType에서 1은 Element입니다.
→ 판단하는 이유는 NodeList가 반환될 때 요소 노드만 반환되지 않고 주석 노드까지 같이 반환되기 때문입니다.
txt += x[i].nodeName + "<br>";
→ nodeType이 element일 경우에 변수에 문자열을 더해줍니다.
for (i = 0; i<x.length; i++){
if(x[i].nodeType == 1){
txt += x[i].nodeName + "<br>";
}
}
조건문을 사용하지 않으면 어떻게 될까요?
for (i = 0; i<x.length; i++){
txt += x[i].nodeName + "<br>";
}
주석 노드까지 출력됩니다.
◡̈
'HTML(or XML) & CSS & JavaScript' 카테고리의 다른 글
[JavaScript] Element의 value 값 바꾸기 (0) | 2022.11.05 |
---|---|
[JavaScript] firstChild와 nextSibling (0) | 2022.11.05 |
[JavaScript] 문자열 합치기 (0) | 2022.11.05 |
[JavaScript] childNodes 속성 (0) | 2022.11.04 |
[JavaScript] Form의 value 값 출력하기 (0) | 2022.10.25 |