(1) HTML ๋ฌธ์ ๋ด ์ค๋ณต๋ id ๊ฐ์ ๊ฐ๋ HTML ์์๊ฐ ์ฌ๋ฌ ๊ฐ ์กด์ฌํ๋๋ผ๋ ์ด๋ ํ ์๋ฌ๋ ๋ฐ์ํ์ง ์๋๋ค.
(2) ์ธ์๋ก ์ ๋ฌ๋ id ๊ฐ์ ๊ฐ์ง HTML ์์๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ, getElementById ๋ฉ์๋๋ ์๋ฌ๋ฅผ ๋ฐ์ํ๋ค.
(3) CSS ์ ํ์ ์ค `p ~ ul {...}`์ p ์์์ ํ์ ์์ ์ค์ p ์์ ๋ฐ๋ก ๋ค์ ์์นํ๋ ul์์๋ฅผ ์ ํํ๋ค.
(4) querySelectorAll ๋ฉ์๋๋ ์ค์๊ฐ์ผ๋ก ๋
ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณ๊ฒฝ์ ๋ฐ์ํ์ง ์๋ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
(1) : true
(2) : false
(3) : false
(4) : true
(1) : (686p) HTML ๋ฌธ์ ๋ด์ ์ค๋ณต๋ id ๊ฐ์ ๊ฐ๋ ์์๊ฐ ์ฌ๋ฌ ๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ, getElementById ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌ๋ id ๊ฐ์ ๊ฐ๋ ์ฒซ ๋ฒ์งธ ์์๋ง ๋ฐํํฉ๋๋ค.
์ฆ, getElementById ๋ฉ์๋๋ ์ธ์ ๋ ๋จ ํ๋์ ์์ ๋
ธ๋๋ง์ ๋ฐํํฉ๋๋ค.
๋ฐ๋ผ์ ์ค๋ณต๋ id๊ฐ ์ฌ๋ฌ ๊ฐ ์กด์ฌํ๋๋ผ๋ ์๋ฌ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค.
(2) : (686p) null์ ๋ฐํํฉ๋๋ค.
(3) : (692p) `p + ul {...}`์ ๋ํ ์ค๋ช
์
๋๋ค.
`p ~ ul {...}`์ p์์์ ํ์ ์์ ์ค์ p์์ ๋ค์ ์์นํ๋ ul ์์๋ฅผ ๋ชจ๋ ์ ํํฉ๋๋ค.
(4) : (698p) HTMLCollection ๊ฐ์ฒด์ ๋ถ์์ฉ์ ํด๊ฒฐํ๊ธฐ ์ํด getElementByTagName, getElementByClassName ๋์ querySelectorAll ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
querySelectorAll ๋ฉ์๋๋ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
NodeList ๊ฐ์ฒด๋ ์ค์๊ฐ์ผ๋ก ๋
ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณ๊ฒฝ์ ํ์ง ์๋ ๊ฐ์ฒด์
๋๋ค.
ํ์ง๋ง childNodes ํ๋กํผํฐ๊ฐ ๋ฐํํ๋ NodeList ๊ฐ์ฒด๋ live ๊ฐ์ฒด๋ก ๋์ํฉ๋๋ค.
๋ฐ๋ผ์ ๋
ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณ๊ฒฝ๊ณผ ์๊ด ์์ด ์์ ํ๊ฒ DOM ์ปฌ๋ ์
์ ์ฌ์ฉํ๊ธฐ ์ํด์๋
HTMLCollection์ด๋ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
(a) ์์ ๋
ธ๋ ์ค์์ ์์ ๋
ธ๋๋ง ๋ชจ๋ ํ์ํ์ฌ HTMLCollection์ ๋ด์ ๋ฐํํ๋ค.
์ด ํ๋กํผํฐ๊ฐ ๋ฐํํ HTMLCollection์๋ ํ
์คํธ ๋
ธ๋๊ฐ ํฌํจ๋์ด ์์ง ์๋ค.
(b) ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ NodeList์ ๋ด์ ๋ฐํํ๋ค.
์ด ํ๋กํผํฐ๊ฐ ๋ฐํํ NodeList์๋ ์์ ๋
ธ๋์ ํ
์คํธ ๋
ธ๋๊ฐ ํฌํจ๋์ด ์๋ค.
(c) ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ๋ฐํํ๋ค.
์ด ํ๋กํผํฐ๊ฐ ๋ฐํํ ๋
ธ๋๋ ํ
์คํธ ๋
ธ๋์ด๊ฑฐ๋ ์์ ๋
ธ๋๋ค.
(d) ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ๋ฐํํ๋ค.
์ด ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
Node.prototype.childNodes : (b)
Element.prototype.children : (a)
Node.prototype.firstChild : (c)
Element.prototype.firstElementChild : (d)
(1) ๋ถ๋ชจ ๋
ธ๋๊ฐ ํ
์คํธ ๋
ธ๋์ธ ๊ฒฝ์ฐ๋ ์๋ค.
(2) Node.ELEMENT_NODE๋ ์์ ๋
ธ๋ ํ์
์ ๋ํ๋ด๋ ์์๋ก 3์ ๋ฐํํ๋ค.
(3) ์์ ๋
ธ๋์ textContent ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นํ ๋, HTML ๋งํฌ์
์ ํ์ฑ๋์ง ์๋๋ค.
(1) : true
(2) : false
(3) : true
(1) : (707p) ํ
์คํธ ๋
ธ๋๋ DOM ํธ๋ฆฌ์ ์ต์ข
๋จ ๋
ธ๋์ด๋ฏ๋ก, ๋ถ๋ชจ ๋
ธ๋๊ฐ ํ
์คํธ ๋
ธ๋์ธ ๊ฒฝ์ฐ๋ ์์ต๋๋ค.
(2) : (709p) 1์ ๋ฐํํฉ๋๋ค.
Node.TEXT_NODE๊ฐ ํ
์คํธ ๋
ธ๋ ํ์
์๋ํ๋ด๋ ์์๋ก 3์ ๋ฐํํฉ๋๋ค.
(3) :(713p)
์์ ๋
ธ๋์ textContent ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นใ
ํ๋ฉด ์์ ๋
ธ๋์ ๋ชจ๋ ์์ ๋
ธ๋๊ฐ ์ ๊ฑฐ๋๊ณ ํ ๋นํ ๋ฌธ์์ด์ด ํ
์คํธ๋ก ์ถ๊ฐ๋ฉ๋๋ค.
์ด ๋ ํ ๋นํ ๋ฌธ์์ด์ HTML ๋งํฌ์
์ด ํฌํจ๋์ด ์๋๋ผ๋ ๋ฌธ์์ด ๊ทธ๋๋ก ์ธ์๋์ด ํ
์คํธ๋ก ์ทจ๊ธ๋ฉ๋๋ค.
์ฆ HTML ๋งํฌ์
์ด ํ์ฑ๋์ง ์์ต๋๋ค.
๐ ๋ ธ๋ ํ์ ์์ https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
<!DOCTYPE html>
<html lang="ko">
<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="app"></div>
<ul id="abc">
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
</body>
<script>
console.log(app);
var app = 1;
console.log(app);
const $abc = document.querySelector('#abc');
console.log($abc.childNodes.length);
console.log($abc.children.length);
</script>
</html>
(1) undefined
(2) 1
id๊ฐ๊ณผ ๋์ผํ ์ด๋ฆ์ ์ ์ญ๋ณ์๊ฐ ์๋์ผ๋ก ํ ๋น๋๊ณ ํด๋น ๋
ธ๋ ๊ฐ์ฒด๊ฐ ์๋์ผ๋ก ๋ง๋ค์ด์ง์ง๋ง
script ์์ ํด๋น ์๋ณ์์ ์ด๋ฆ์ผ๋ก์ค์ ๋ ๋ณ์๊ฐ ์์ผ๋ฉด ๋
ธ๋ ๊ฐ์ฒด๊ฐ ํ ๋น๋์ง ์๋๋ค.
(3) 7
(4) 3
Node.prototype.childNodes์ ์์ ๋
ธ๋์ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ๊ณ ,
Element.prototype.children์ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
<!DOCTYPE html>
<html lang="ko">
<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>
<style>
.green {
color : green;
}
.blue {
color : blue;
}
</style>
<body>
<div class="green">a</div>
<div class="green">b</div>
<div class="green">c</div>
</body>
<script>
let $items = document.querySelectorAll('.green');
console.log($items);
for(let i =0; i < $items.length; i++){
$items[i].className = "blue";
}
$items = document.getElementsByClassName('blue');
console.log($items);
for(let i =0; i < $items.length; i++){
$items[i].className = "green";
}
</script>
</html>
์ด๋ก์ a
ํ๋์ b
์ด๋ก์ c
HTMLCollection์ ๋
ธ๋์ ๊ฐ์ฒด ์ํ๋ณํ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ์ด์์๋ ๊ฐ์ฒด์ด๊ณ ,
Nodelist๋ childNodes ํ๋กํผํฐ๊ฐ ๋ฐํํ๋ NodeList ๊ฐ์ฒด๊ฐ ์๋ ์ด์ none-alive ๊ฐ์ฒด์ด๋ค.
๋ฐ๋ผ์ ์ค์๊ฐ์ผ๋ก ๋ฐ๋๊ธฐ ๋๋ฌธ์ $items์ ์ธ๋ฑ์ค๊ฐ 3 => 2๋ก ๋ณํํด 1, 3๋ฒ์งธ ์์๋ง
๋ฐ๋๊ฒ ๋๋ค.
๋ ๋ค ์์ธกํ์ง ๋ชปํ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์๊ธฐ ๋๋ฌธ์, ๋ฐฐ์ด๋ก ๋ณํํด ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
(1) getElementById๋ก ์์๋ฅผ ๊ฐ์ ธ์ฌ ๋ ํด๋น id ๊ฐ์ ๊ฐ์ง ์์๊ฐ ํ ๊ฐ ์ด์์ด๋ผ๋ฉด
์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค.
(2) Node.prototyle.nextSibling์ ํ์ ๋
ธ๋ ์ค์์ ๋ค์ ํ์ ์์ ๋
ธ๋๋ฅผ ๋ฐํํ๋ค.
(3)๋
ธ๋ ํ์ ํ๋กํผํฐ๋ ์ฝ๊ธฐ ์ ์ฉ ์ ๊ทผ์ ํ๋ฌํผํฐ์ด๊ณ , ๊ฐ์ ๋์
ํ๋ฉด ์๋ฌ๋ฅผ ๋ฐ์์ํจ๋ค.
(1) false
getElementById๋ก ์์๋ฅผ ๊ฐ์ ธ์ฌ ๋ ํด๋น id ๊ฐ์ ๊ฐ์ง ์์๊ฐ ํ ๊ฐ ์ด์์ด๋ผ๋ฉด
์๋ฌ๋ฅผ ๋ฐ์์ํค์ง ์๊ณ ๋งจ ์ฒ์์ ์์๋ฅผ ๊ฐ์ ธ์จ๋ค.
(2) false
Node.prototype.nextSibling์ ํ์ ๋
ธ๋ ์ค์์ ๋ค์ ํ์ ๋
ธ๋๋ฅผ ๋ฐํํ๋๋ฐ, ์์ ๋
ธ๋์ผ ์๋ ์๊ณ
ํ
์คํธ ๋
ธ๋์ผ ์๋ ์๋ค. ์ด๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํด์๋
Element.prototype.nextElementSibling์ ์ฌ์ฉํ๋ฉด ๋๋ค.
(3) false
๋
ธ๋ ํ์ ํ๋กํผํฐ๋ ์ฝ๊ธฐ ์ ์ฉ ์ ๊ทผ์ ํ๋ฌํผํฐ์ด๊ณ , ๊ฐ์ ๋์
ํ๋ฉด ์๋ฌ๋ฅผ ๋ฐ์์ํค์ง ์๋๋ค.
(1) : ๋ฌธ์ ๋
ธ๋(document ๊ฐ์ฒด)๋ html๋ฌธ์์ <html>๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
(2) : ๋
ธ๋ ๊ฐ์ฒด๋ 4๊ฐ์ง(๋ฌธ์ ๋
ธ๋, ์์ ๋
ธ๋, ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋, ํ
์คํธ ๋
ธ๋)๋ก ๊ตฌ์ฑ๋์ด ์๋ค.
(3) : textContent ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด ํ
์คํธ๋ก ์ถ๊ฐ๋์ด์ HTML ๋งํฌ์
์ด ํ์ฑ๋์ง ์๋๋ค.
(1) : false (p680)
๋ฌธ์๋
ธ๋(Document ๊ฐ์ฒด)๋ DOM ํธ๋ฆฌ์ ๋ฃจํธ ๋
ธ๋์ด๊ณ , <html>์ HTML ๋ฌธ์์ ๋ฃจํธ ์์์
๋๋ค.
(2) : false (p680, p709)
๊ต์ฌ p680์์ 12์ข
๋ฅ๋ผ๊ณ ์ธ๊ธํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ 12๊ฐ์ ๋
ธ๋ ํ์
์ p709๋ฅผ ์ฐธ๊ณ ํ์ฌ nodeType์ ์ํด 12์ข
๋ฅ๋ฅผ ์ ์ ์์ต๋๋ค. (๋งํฌ ์ฐธ๊ณ )
(3) : true (p713)
textContent์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ์ ๊ฑฐํ๊ณ ํด๋น ๋ฌธ์์ด์ ์ถ๊ฐํ๋ค.
๋ฌธ์์ด์ html ํ๊ทธ๊ฐ ์์ด๋ ํ์ฑ๋์ง ์๊ณ ๋ฌธ์์ด๋ก ์ทจ๊ธ๋๋ค.
๋
ธ๋(Node)
์ ์์(Element)
๊ฐ ๋์ผํ ์ง ํ๋จํ์์ค. ๋ค๋ฅด๋ค๋ฉด ์ฐจ์ด์ ์ ์์ฑํ์์ค.
๋
ธ๋์ ์์๋ ๋์ผํ์ง ์๋ค.
๋
ธ๋(Node)๋ ์ฌ๋ฌ DOM ํ์
์ด ์์ํ๋ ์ธํฐํ์ด์ค์ด๋ค.
๋ํ์ ์ธ ํ๋กํผํฐ๋ก baseURI, childNodes, firstChild, textContent ๋ฑ์ด ์๋ค.
๋ฉ์๋๋ก๋ appendChild(), hasChildNodes(), removeChild() ๋ฑ์ด ์๋ค.
์์(Element)๋ Document ๊ฐ์ฒด ์์ ๋ชจ๋ ๊ฐ์ฒด๊ฐ ์์ํ๋ ์ธํฐํ์ด์ค์ด๋ค.
๋ํ์ ์ธ ํ๋กํผํฐ๋ก classList, innerHTML, tagName ๋ฑ์ด ์๋ค.
๋ฉ์๋๋ก๋ addEventListener(), closest(), querySelector() ๋ฑ์ด ์๋ค.
์์๋ ๋
ธ๋๋ฅผ ์์๋ฐ๋๋ค.
๋ชจ๋ DOM API๊ฐ ์ฌ๋ฌ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๊ธฐ ์ํ DOM ์ปฌ๋ ์
๊ฐ์ฒด์ด๋ค.
๋ํ ๋ชจ๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฉด์ ์ดํฐ๋ฌ๋ธ์ด๋ค.
HTMLCollection
ํญ์ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ๊ฐ์ฒด์ด๋ฏ๋ก ์ํ๋ฅผ ํ๋ฉด์ HTMLCollection์ ๋
ธ๋๋ฅผ ์กฐ์ํ๋ ๊ฒฝ์ฐ ์ํ๋ ๋๋ก ์คํํ์ง ์์ ์ ์๋ค.
๋ฐ๋ผ์ ์คํ๋ ๋ ๋ฌธ๋ฒ์ด๋ Array.from()์ผ๋ก ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํ๋ค.
NodeList
childNodesํ๋กํผํฐ๋ฅผ ์ ์ธํ๊ณ ์ค์๊ฐ์ ๋ฐ์ํ์ง ์๋ ๊ฐ์ฒด์ด๋ฏ๋ก ์์ ๋กญ๊ฒ ์ํํ๊ณ ๋
ธ๋๋ฅผ ์ ์ดํ๋ฉด ๋๋ค.
- p695 ~ 699
- ๐HTMLCollection (MDN)
- ๐NodeList (MDN)
๋ชจ๋ ๋
ธ๋๊ฐ์ฒด๋ EventTarget์ ์์๋ฐ๊ธฐ๋๋ฌธ์ EventTarget์ ์ ์๋ addEventListener๋ฉ์๋๋ฅผํตํด ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํฌ ์ ์๋ค.
์ฑ ์์ HTMLCollection๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ์ง ์๋๊ฒ์ ๊ถ์ฅํ๋๋ฐ, ๊ทธ ์ด์ ์๋ํด์ ์์ ํ๊ณ (1) HTMLCollection๋ฅผ ๋ฐํํ๋ DOM API(2)๋ฅผ ์์ฑํ์ธ์.
(1) HTMLCollection๊ฐ์ฒด๋ ์ค์๊ฐ์ผ๋ก ๋
ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณ๊ฒฝ์ ๋ฐ์ํ๋ live DOM ์ปฌ๋ ์
๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๋์์ ํ์
ํ๊ธฐ๊ฐ ์ด๋ ต๋ค. ๋ฐ๋ผ์ ์ข๋ ์์ ํ๊ฒ HTMLCollection๊ฐ์ฒด๊ฐ ์ดํฐ๋ฌ๋ธ์์ ๊ฐ์ํ์ฌ ์คํ๋ ๋ ๋ฌธ๋ฒ์ผ๋ก ๋ฐฐ์ด๋ก ๋ณํํ ์ ์ฉํ ๋ฐฐ์ด์ ๊ณ ์ฐจํจ์๋ฅผ ์ฌ์ฉํ์.
(2) getElementsByTagName, getElementsByClassName
<div class="container">
ํ
์คํธ
<div></div>
</div>
<script>
const $container = document.querySelector(".container");
console.log($container.nodeValue); // (1)
console.log($container.textContent[0]); // (2)
console.log($container.innerText[0]); // (3)
</script>
(1) null
(2)
(3) ํ
- HTML5๋ innerHTML ํ๋กํผํฐ๋ก ์ฝ์
๋ script์์ ๋ด์ ์๋ฐ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ์คํํ์ง ์๋๋ค.
- DOcument.prototype.createElement(tagName) ๋ฉ์๋๋ก ์์๋
ธ๋๋ฅผ ์์ฑํ๋ ๊ณผ์ ์์ ๋ฆฌํ๋ก์ฐ,๋ฆฌํ์ธํธ๊ฐ ์คํ๋๋ค.
- Document.prototype.createDocumentFragment ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ถํ์ํ ์ปจํ
์ด๋ ํ๊ทธ๊ฐ ๋์ ์ถ๊ฐ๋๋๊ฒ์ ๋ฐฉ์ง ํ ์ ์๋ค.
(1) true - p.717
(2) false - ์ดํ Node.prototype.appendChild๋ฉ์๋๋ฅผ ํตํด ๋ณ๋๋ก DOM์ ์ถ๊ฐํด์ฃผ๋ ๊ณผ์ ์ ๊ฑฐ์น ๋ ๋ฆฌํ๋ก์ฐ,๋ฆฌํ์ธํธ๊ฐ ์คํ๋๋ค.
(3) true - p.726
- HTMLํ๊ทธ(div,span,inputโฆ)์ ์ดํธ๋ฆฌ๋ทฐํธ๋ ๋ณํ๋ DOM์์์ ํ๋กํผํฐ๋ก ์ฐธ์กฐํ ์ ์๋ค.
- HTMLํ๊ทธ(div,span,inputโฆ)์ ์ดํธ๋ฆฌ๋ทฐํธ์ ์ญํ ์ ๋ณํ๋ DOM์์์ ํ๋กํผํฐ์ ์ด๊ธฐ๊ฐ์ ์ง์ ํ๋ค.
- ๋ชจ๋ DOMํ๋กํผํฐ๋ ์ฌ์ฉ์ ์
๋ ฅ์์ํด ๋ณ๊ฒฝ๋ ์ต์ ์ํ๋ฅผ ๊ด๋ฆฌํ๋ค.
(1) false - attribuesํ๋กํผํฐ๋ก ์ ๊ทผ๊ฐ๋ฅ.
(2) true - p. 738
(3) false - ์ฌ์ฉ์ ์
๋ ฅ์์ํ ์ํ ๋ณํ์ ๊ด๋ จ์๋ DOMํ๋กํผํฐ๋ง ์ต์ ์ํ ๊ฐ์ผ๋ก ์ ์ง.
1. innerHTML = โ<div>1<div>โ; innerHTML += โ<div>2<div>โ; ์ผ๋ <div>1<div>์ ์ ์งํ๊ณ ์๋ก์ด div์์๋ง ์ถ๊ฐ๋๋ค.
2. ๋ชจ๋ HTML ์ดํธ๋ฆฌ๋ทฐํธ๋ DOM ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๋ค.
3. ์์ ๋
ธ๋์ ์ด๊ธฐ ์ํ๋ DOM ๋
ธ๋๊ฐ ๊ด๋ฆฌํ๋ฉฐ, ์ต์ ์ํ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋๊ฐ ๊ด๋ฆฌํ๋ค.
###๋ต์ ์์ฑ
(1) false
innerHTML ํ๋กํผํฐ๋ HTML ๋งํฌ์
๋ฌธ์๋ฅผ ํ ๋นํ๋ ๊ฒฝ์ฐ ์์ ๋
ธ๋์ ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ์ ๊ฑฐํ๊ณ ํ ๋นํ HTML ๋งํฌ์
๋ฌธ์์ด์ ํ์ฑํ์ฌ DOM์ ๋ณ๊ฒฝํ๋ค.
(2) false
๋๋ถ๋ถ์ ์ดํธ๋ฆฌ๋ทฐํธ๋ DOM ๋
ธ๋๋ฅผ ๊ฐ์ง์ง๋ง ์์ธ๋ ์กด์ฌํ๋ค. ์๋ฅผ ๋ค์ด textContent ํ๋กํผํฐ๋ ๋์ํ๋ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ๊ฐ์ง์ง ์๋๋ค.
(3) false
์์ ๋
ธ๋์ ์ด๊ธฐ ์ํ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋๊ฐ ๊ด๋ฆฌํ๋ฉฐ, ์ต์ ์ํ๋ DOM ๋
ธ๋๊ฐ ๊ด๋ฆฌํ๋ค.
<!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="app"></div>
<script>
const $app = document.querySelector("#app");
$app.innerHTML = '<script>alert("hello")</script>'
$app.innerHTML = `<img src='x' onerror="alert('bye')"/>`;
</script>
</body>
</html>
alert('bye') ๊ฐ ์คํ๋๋ค. script ํ๊ทธ๋ html5์์๋ ๋ง์์ฃผ๊ธฐ ๋๋ฌธ์ ์คํ๋์ง ์๊ณ ,
์ด๋ฏธ์ง์ onerror๋ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ ๋ ์ผ์ด๋๋ ๊ฒ์ผ๋ก, ์ฝ๋์์ ์ด๋ฏธ์ง๋ฅผ
๊ฐ์ ธ์ฌ ์ ์๊ธฐ ๋๋ฌธ์ onerror ์์ ํจ์๊ฐ ์คํ๋๋ค. ์ด๋ฌํ ๋ฐฉ์์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์,
innerHTML์ xss๊ณต๊ฒฉ์ ์ํํ๋ค.
<!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="app" style="width : 100px; height : 100px"></div>
<script>
const $app = document.querySelector("#app");
$app.style.backgroundColor = "red";
console.log($app.style.backgroundColor); // (3)
console.log($app.style.opacity); // (4)
</script>
<style>
#app {
background-color: blue;
height : 200px;
opacity: 0.5;
}
</style>
</body>
</html>
(1) width : 100px
(2) height : 100px
(3) background-color : red
style ํ๋กํผํฐ๋ ์ธ๋ผ์ธ ์คํ์ผ๋ก ์ ์ฉ๋๊ธฐ ๋๋ฌธ์ ์ผ๋ฐ์ ์ธ css ๋ณด๋ค ์ฐ์ ์์์ ์๋ค.
(4) - 1 ์ฝ์ ๊ฒฐ๊ณผ : red
(4) - 2 ์ฝ์ ๊ฒฐ๊ณผ : ์๋ฌด๊ฒ๋ ์ถ๋ ฅ๋์ง ์์
style ํ๋กํผํฐ๋ ์ธ๋ผ์ธ ์คํ์ผ๋ง ๋ฐํํ๋ค. ๋ฐ๋ผ์ ํด๋์ค๋ฅผ ์ ์ฉํ ์คํ์ผ์ด๋
์์์ ํตํด ์๋ฌต์ ์ผ๋ก ์ ์ฉ๋ ์คํ์ผ์ style ํ๋กํผํฐ๋ก ์ฐธ์กฐํ ์ ์๋ค.
๋ฐ๋ผ์ html ์์์ ์ ์ฉ๋์ด์๋ ๋ชจ๋ css ์คํ์ผ์ ์ฐธ์กฐํด์ผํ ๊ฒฝ์ฐ getComputedStyle
๋ฉ์๋๋ฅผ ์ด์ฉํ๋ค.
์ ๋ต : ๋ฐ์ดํฐ(htmlString)์ ํ์ฑํ๊ธฐ ๋๋ฌธ์ด๋ค.
๋ ํค์๋๋ ์ฃผ์ด์ง ๋ฐ์ดํฐ(htmlString)์ ํ์ฑํ์ฌ document์ ์๋ก์ด ์์๋ฅผ ์ถ๊ฐ๋ฅผ ํฉ๋๋ค.
ํ์ฑํ๋ ๊ณผ์ ์์ ์
์ฑ script๋ฅผ ์ฃผ์
ํ๋ XSS ๊ณต๊ฒฉ์ ๋
ธ์ถ๋ฉ๋๋ค.
์ด๋ฅผ ๋ง๊ธฐ ์ํด HTML5๋ถํฐ๋ ๋ฐ์ดํฐ(htmlString)์ script ํ๊ทธ๊ฐ ๋ค์ด๊ฐ ๊ฒฝ์ฐ ์คํํ์ง ์๋๋ก ํ์์ต๋๋ค.
ํ์ง๋ง <img src='x' onerror='alert(1)'> ๊ฐ์ ๊ฒฝ์ฐ๋ ๋ง์ง ๋ชปํฉ๋๋ค.
๋ฐ๋ผ์ ํด๋น ํค์๋๋ฅผ ์ง์ํ์๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
๊ต์ฌ์์ ์์ ๋
ธ๋์ ์ํ๋ฅผ 2๊ณณ์์ ๊ด๋ฆฌํ๋ค๊ณ ๋ช
์๋์ด ์์ต๋๋ค. ์ด๊ธฐ ์ํ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋
๊ฐ ๊ด๋ฆฌํ๋ฉฐ, ์ต์ ์ํ๋ DOM ํ๋กํผํฐ
๊ฐ ๊ด๋ฆฌํฉ๋๋ค. ์ 2๊ฐ์ง ์ํ๋ก ๊ด๋ฆฌํ๋ ์ด์ ๋ฅผ ์์ฑํ์์ค.
์ด๊ธฐ ์ํ๋ฅผ ๊ด๋ฆฌํ์ง ์์ผ๋ฉด ์นํ์ด์ง๋ฅผ ์ฒ์ ํ์ํ๊ฑฐ๋ ์๋ก๊ณ ์นจํ ๋ ์ด๊ธฐ ์ํ๋ฅผ ํ์ํ ์ ์๊ธฐ ๋๋ฌธ์
๋๋ค.
๋ฐ๋ผ์ ์ฌ์ฉ์ ์
๋ ฅ์ ๋ฐ๋ฅธ ์ต์ ์ํ๋ฅผ ๊ด๋ฆฌํด์ผ ํ๋ฉด์, ์ด๊ธฐ ์ํ๋ฅผ ๊ด๋ฆฌํด์ผ ํ๋ค.
- p739
๋
ธ๋๋ฅผ ์กฐ์ํ๋ ๋ฉ์๋ ์ค์ insertBefore
๋ฅผ ์ด์ฉํ์ฌ ๋
ธ๋๋ฅผ ์ด๋ํ๋ฉด ์๋์ ๋ธ๋ผ์ฐ์ ๋ ๋๋ง์์ ์ด๋ ๋ถ๋ถ๋ถํฐ ์์ํ๋์ง ๊ณ ๋ฅด์์ค.
(1) : DOM tree
(2) : CSSOM tree
(3) : Render tree
(4) : Layout (Reflow)
(5) : Paint (Repaint)
Layout
๋
ธ๋์ ์ด๋๋ฟ๋ง ์๋๋ผ ์ถ๊ฐ, ์ญ์ , ๋ณ๊ฒฝ ๋ฑ ๋
ธ๋๋ฅผ ์กฐ์ํ๋ ๊ฒฝ์ฐ, ๋ณ๊ฒฝ์ฌํญ์ ๋ฐ์ํ๊ธฐ ์ํด Layout(Reflow)๋ถํฐ ์์ํ๊ฒ ๋ฉ๋๋ค.
๋ค๋ง, DOM tree์ ๋ณ๊ฒฝ์ฌํญ์ ๋ฐ์ํ์ง๋ง ๋ค์ DOM tree๋ฅผ ๋ง๋ค์ง๋ ์์ต๋๋ค.
innerHTML ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ DOM ์กฐ์์ ๊ตฌํ์ด ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ด์ง๋ง ๋ค์ํ ๋จ์ ์ด ์กด์ฌํฉ๋๋ค.
(1) : ํฌ๋ก์ค ์ฌ์ดํธ ์คํฌ๋ฆฝํ
๊ณต๊ฒฉ์ ์ทจ์ฝํฉ๋๋ค.
(2) : ์์ ๋
ธ๋์ innerHTML ํ๋กํผํฐ์ HTML ๋งํฌ์
๋ฌธ์์ด์ ํ ๋นํ๋ ๊ฒฝ์ฐ,
์์ ๋
ธ๋์ ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ์ ๊ฑฐํ๊ณ ํ ๋นํ HTML ๋งํฌ์
๋ฌธ์์ด์ ํ์ฑํ์ฌ DOM์ ๋ณ๊ฒฝํฉ๋๋ค.
(3) : ์๋ก์ด ์์๋ฅผ ์ฝ์
ํ ๋ ์ฝ์
๋ ์์น๋ฅผ ์ง์ ํ ์ ์์ต๋๋ค.
<html>
<head>
<style>
.box{
width: 100px;
height: 100px;
background-color: royalblue;
}
.red { color: red}
.blue {color: blue}
</style>
</head>
<body>
<div class="box red">Hello!</div>
<script>
const $box=document.querySelector('red')
</script>
</body>
</html>
(1) $box.classList.add('DeepDive');
(2) $box.classList.contains('box');
(3) $box.classList.remove('blue');
(1) : add ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌํ 1๊ฐ ์ด์์ ๋ฌธ์์ด์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ผ๋ก ์ถ๊ฐํฉ๋๋ค.
: class = "box red DeepDive"
(2) : contains ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ฉด ํด๋์ค๊ฐ class ์ดํธ๋ฆฌ๋ทฐํธ์ ํฌํจ๋์ด ์๋์ง ํ์ธํฉ๋๋ค.
: true
(3) : ์ธ์๋ก ์ ๋ฌํ 1๊ฐ ์ด์์ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ ํด๋์ค๋ฅผ class ์ดํธ๋ฆฌ๋ทฐํธ์์ ์ญ์ ํฉ๋๋ค.
: ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ ํด๋์ค๊ฐ class ์ดํธ๋ฆฌ๋ทฐํธ์ ์์ผ๋ฉด ์๋ฌ ์์ด ๋ฌด์ํฉ๋๋ค.
: class = "box blue"
<html>
<body>
<div>test</div>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
<script>
const $fruits = document.getElementById('fruits')
const $li = document.createElement('li')
$li.appendChild(document.createTextNode('Orange'))
$fruits.insertBefore($li, document.querySelector('div')) // (1)
</script>
</body>
</html>
DOMException
insertBefore ๋ฉ์๋๋ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋
ธ๋๋ฅผ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋
ธ๋ ์์ ์ฝ์
ํฉ๋๋ค.
๋ฐ๋ผ์ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ ๋
ธ๋๋ ๋ฐ๋์ insertBefore ๋ฉ์๋๋ฅผ ํธ์ถํ ๋
ธ๋์ ์์ ๋
ธ๋์ด์ด์ผ ํฉ๋๋ค.
์ฆ $fruits ์์์ ์์๋
ธ๋์ด์ด์ผ ํฉ๋๋ค.