-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryselector.html
56 lines (48 loc) · 1.5 KB
/
queryselector.html
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
<!--쿼리셀렉터 연습-->
querySelector(#id) => id값 id를 가진 요소 접근
querySelector(.class) => class값 class를 가진 요소 접근
querySelectorAll("#id, .class");
== nodeList를 반환하기 때문에 for문 사용
var section = document.querySelector("#sections .section");
section.style.border = "1px solid #ff0000";
const sections = document.querySelectorAll("#sections , #sections .section");
console.log(sections.constructor.name);
for (const i = 0; i < sections.length; i++) {
const item = sections.item(i);
item.style.border = "1px solid #ff0000";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="sec">
<ol class="set">
<h1>No.1</h1>
<li>1-1</li>
<li>1-2</li>
<li>1-3</li>
</ol>
<ol class="set">
<h1>No.2</h1>
<li>2-1</li>
<li>2-2</li>
<li>2-3</li>
</ol>
</div>
<script>
/* var section = document.querySelector("#sec .set");
section.style.border = "1px solid #ff0000"; */ /*쿼리셀렉터기 때문에 가장 위에있는 클래스만 적용*/
const sections = document.querySelectorAll("#sec, #sec .set");
console.log(sections.constructor.name);
for (const i = 0; i < sections.length; i++) {
const item = sections.item(i);
item.style.border = "1px solid #ff0000"
}
</script>
</body>
</html>