-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs12_DOM.html
More file actions
63 lines (54 loc) · 2.69 KB
/
js12_DOM.html
File metadata and controls
63 lines (54 loc) · 2.69 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h3>DOM을 기반으로 한 html 문서 구조 access하기</h3>
1. 기본 html 구조 개발 <br>
2. DOM api로 각 elemet or text 검색 및 활용 <br>
3. 상속 : 조상, 부모, 자식 관점에서 html 문서( 속성 상속 관계에서 예외) <br>
<hr><br>
<p id="one">
<p id="cat">
자식 text
<p id="dog">
멍멍
</p>
</p>
</p>
br 즉 new line은 html 관점에서 text 자식으로 간주
<script>
console.log(document.getElementsByTagName("p").length);
console.log(document.getElementsByTagName("p").item);
console.log(document.getElementsByTagName("p"));
console.log("--- 1 ---");
console.log(document.getElementsByTagName("p"));
console.log(document.getElementsByTagName("p")[0]);
console.log(document.getElementsByTagName("p")[1]);
console.log(document.getElementsByTagName("p")[2]);
console.log(document.getElementsByTagName("p")[3]);
console.log(document.getElementsByTagName("p")[4]);
console.log("--- 2 ---");
console.log(document.getElementsByTagName("p")[1]);
console.log(document.getElementsByTagName("p").innerText);//자식 text
console.log("--- 3 --- : br로 발생된 new line도 text node로 인식");
console.log(document.getElementsByTagName("p").childNodes);//undefined js12_DOM.html:43
console.log(document.getElementsByTagName("p")[0].childNodes);//NodeList
console.log(document.getElementsByTagName("p")[1].childNodes);//NodeList
console.log(document.getElementsByTagName("p")[2].childNodes);//NodeList
console.log(document.getElementsByTagName("p")[0].childNodes.length);//1
console.log(document.getElementsByTagName("p")[1].childNodes.length);//1
console.log(document.getElementsByTagName("p")[2].childNodes.length);//1
console.log("--- 4 --- : html문서 순회하기");
//id가 one이 보유하고 있는 하위 자식 중에 p라는 tag를 찾아서 그 하위의 text 값 출력
console.log(document.getElementById("one").nodeName);//현 element 이름
console.log(document.getElementById("one").nodeValue);//null, text값에 한해서만 반환
console.log(document.getElementById("one").firstChild);//
console.log(document.getElementById("one").lastChild);//
console.log(document.getElementById("one").lastChild.previousSibling);//
</script>
</body>
</html>