39์ฅ DOM
39.5 ์์ ๋
ธ๋์ ํ
์คํธ ์กฐ์
39.5.1 nodeValue
Node.prototype.nodeValue ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ค.
๋ ธ๋ ๊ฐ์ฒด์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด ๋ ธ๋ ๊ฐ์ฒด์ ๊ฐ์ ๋ฐํํ๋ค. element์ผ ๊ฒฝ์ฐ Null, Text๋ ธ๋์ผ ๊ฒฝ์ฐ text ๊ฐ์ ๋ฐํํ๋ค.
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// ๋ฌธ์ ๋
ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ค.
console.log(document.nodeValue); // null
// ์์ ๋
ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ค.
const $foo = document.getElementById("foo");
console.log($foo.nodeValue); // null
// ํ
์คํธ ๋
ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ค.
const $textNode = $foo.firstChild;
console.log($textNode.nodeValue); // Hello
</script>
</html>
39.5.2 textContent
Node.prototype.textContent ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก์ ์์ ๋ ธ๋์ ํ ์คํธ์ ๋ชจ๋ ์์ ๋ ธ๋์ ํ ์คํธ๋ฅผ ๋ชจ๋ ์ทจ๋ํ๊ฑฐ๋ ๋ณ๊ฒฝํ๋ค.
์์ ๋ ธ๋์ textContent ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด ์์ ๋ ธ๋์ ์ฝํ ์ธ ์์ญ ๋ด์ ํ ์คํธ๋ฅผ ๋ชจ๋ ๋ฐํํ๋ค.

<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// #foo ์์ ๋
ธ๋๋ ํ
์คํธ ๋
ธ๋๊ฐ ์๋๋ค.
console.log(document.getElementById("foo").nodeValue); // null
// #foo ์์ ๋
ธ๋์ ์์ ๋
ธ๋์ธ ํ
์คํธ ๋
ธ๋์ ๊ฐ์ ์ทจ๋ํ๋ค.
console.log(document.getElementById("foo").firstChild.nodeValue); // Hello
// span ์์ ๋
ธ๋์ ์์ ๋
ธ๋์ธ ํ
์คํธ ๋
ธ๋์ ๊ฐ์ ์ทจ๋ํ๋ค.
console.log(document.getElementById("foo").lastChild.firstChild.nodeValue); // world!
// #foo ์์ ๋
ธ๋์ ํ
์คํธ๋ฅผ ๋ชจ๋ ์ทจ๋ํ๋ค, ์ด๋ HTML ๋งํฌ์
์ ๋ฌด์๋๋ค.
console.log(document.getElementById("foo").textContent); //Hello world
</script>
</html>
39.6 DOM ์กฐ์
DOM ์กฐ์์ ์๋ก์ด ๋ ธ๋๋ฅผ ์์ฑํ์ฌ DOM์ ์ถ๊ฐํ๊ฑฐ๋ ๊ธฐ์กด ๋ ธ๋๋ฅผ ์ญ์ ๋๋ ๊ต์ฒดํ๋ ๊ฒ์ ๋งํ๋ค.
DOM ์กฐ์์ ์ํด DOM์ ์๋ก์ด ๋ ธ๋๊ฐ ์ถ๊ฐ๋๊ฑฐ๋ ์ญ์ ๋๋ฉด ๋ฆฌํ๋ก์ฐ์ ๋ฆฌํ์ธํธ๊ฐ ๋ฐ์ํ๋ ์์ธ์ด ๋๋ฏ๋ก ์ฑ๋ฅ์ ์ํฅ์ ์ค๋ค.
39.6.1 innerHTML
Element.prototype.innerHTML ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก์ ์์ ๋ ธ๋์ HTML ๋งํฌ์ ์ ์ทจ๋ํ๊ฑฐ๋ ๋ณ๊ฒฝํ๋ค.
์๋์ฒ๋ผ innerHTML ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ฉด HTML ๋งํฌ์ ๋ฌธ์์ด๋ก ๊ฐ๋จํ DOM ์กฐ์์ด ๊ฐ๋ฅํ๋ค.
<html> <body> <ul id="fruits"> <li class="apple">Apple</li> </ul> </body> <script> const $fruits = document.getElementById("fruits"); // ๋ ธ๋ ์ถ๊ฐ $fruits.innerHTML += '<li class="banana">Banana</li>'; // ๋ ธ๋ ๊ต์ฒด $fruits.innerHTML = '<li class="orange">Orange</li>'; // ๋ ธ๋ ์ญ์ $fruits.innerHTML = ""; </script> </html>
innerHTML ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ DOM ์กฐ์์ ๊ตฌํ์ด ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ด๋ผ๋ ์ฅ์ ์ด ์์ง๋ง XSS(Cross Site Script) ๊ณต๊ฒฉ์ ์ทจ์ฝํ๋ค๋ ๋จ์ ์ด ์๋ค.
39.6.2 insertAdjacentHTML ๋ฉ์๋
Element.prototype.insertAdjacentHTML(position, DOMString) ๋ฉ์๋๋ ๊ธฐ์กด ์์๋ฅผ ์ ๊ฑฐํ์ง ์์ผ๋ฉด์ ์์น๋ฅผ ์ง์ ํด ์๋ก์ด ์์๋ฅผ ์ฝ์ ํ๋ค.
<html> <body> <!-- beforebegin --> <div id="foo"> <!-- afterbegin --> text <!-- beforeend --> </div> <!-- afterend --> </body> <script> const $foo = document.getElementById("foo"); $foo.insertAdjacentHTML("beforebegin", "<p>beforebegin</p>"); $foo.insertAdjacentHTML("afterbegin", "<p>afterbegin</p>"); $foo.insertAdjacentHTML("beforeend", "<p>beforeend</p>"); $foo.insertAdjacentHTML("afterend", "<p>afterend</p>"); </script> </html>
39.6.3 ๋
ธ๋ ์์ฑ๊ณผ ์ถ๊ฐ
DOM์ ๋ ธ๋๋ฅผ ์ง์ ์์ฑ/์ฝ์ /์ญ์ /์นํํ๋ ๋ฉ์๋๋ ์ ๊ณตํ๋ค.
์๋์ ๊ณผ์ ์ ํตํด์ ๋ ธ๋์ ์์ฑ๊ณผ ์ถ๊ฐ๊ฐ ์ด๋ฃจ์ด์ง๋ค. (์ถํ ์ค๋ช ์ถ๊ฐ ์์ )
- ์์ ๋ ธ๋ ์์ฑ - ํ ์คํธ ๋ ธ๋ ์์ฑ - ํ ์คํธ ๋ ธ๋๋ฅผ ์์ ๋ ธ๋์ ์์ ๋ ธ๋๋ก ์ถ๊ฐ - ์์ ๋ ธ๋๋ฅผ DOM์ ์ถ๊ฐ
<html> <body> <ul id="fruits"> <li>Apple</li> <!-- <li>Banana</li> --> </ul> </body> <script> const $fruits = document.getElementById("fruits"); // 1. ์์ ๋ ธ๋ ์์ฑ const $li = document.createElement("li"); // 2. ํ ์คํธ ๋ ธ๋ ์์ฑ const textNode = document.createTextNode("Banana"); // 3. ํ ์คํธ ๋ ธ๋๋ฅผ $li ์์ ๋ ธ๋์ ์์ ๋ ธ๋๋ก ์ถ๊ฐ $li.appendChild(textNode); // 4. $li ์์ ๋ ธ๋๋ฅผ #fruits ์์ ๋ ธ๋์ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก ์ถ๊ฐ $fruits.appendChild($li); </script> </html>
39.6.4 ๋ณต์์ ๋
ธ๋ ์์ฑ๊ณผ ์ถ๊ฐ
DOM์ ์ฌ๋ฌ๋ฒ ๋ณ๊ฒฝํ๋ ๊ฒ์ ๊ฐ๊ฐ ๋ฆฌํ๋ก์ฐ์ ๋ฆฌํ์ธํ ์ ์ผ๊ธฐ์ํจ๋ค. ์ด๋ฅผ ํผํ๊ธฐ ์ํด ์ปจํ ์ด๋ ์์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
<html> <body> <ul id="fruits"></ul> </body> <script> const $fruits = document.getElementById("fruits"); // DocumentFragment ๋ ธ๋ ์์ฑ const $fragment = document.createDocumentFragment(); ["Apple", "Banana", "Orange"].forEach((text) => { // 1. ์์ ๋ ธ๋ ์์ฑ const $li = document.createElement("li"); // 2. ํ ์คํธ ๋ ธ๋ ์์ฑ const textNode = document.createTextNode(text); // 3. ํ ์คํธ ๋ ธ๋๋ฅผ $li ์์ ๋ ธ๋์ ์์ ๋ ธ๋๋ก ์ถ๊ฐ $li.appendChild(textNode); // 4. $li ์์ ๋ ธ๋๋ฅผ DocumentFragment ๋ ธ๋์ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก ์ถ๊ฐ $fragment.appendChild($li); }); // 5. DocumentFragment ๋ ธ๋๋ฅผ #fruits ์์ ๋ ธ๋์ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก ์ถ๊ฐ $fruits.appendChild($fragment); </script> </html>
๋จผ์ DocumentFragment ๋ ธ๋๋ฅผ ์์ฑํ๊ณ DOM์ ์ถ๊ฐํ ์์ ๋ ธ๋๋ฅผ ์์ฑํ์ฌ DocumentFragment ๋ ธ๋์ ์์ ๋ ธ๋๋ก ์ถ๊ฐํ ๋ค์, DocumentFragment ๋ ธ๋๋ฅผ ๊ธฐ์กด DOM์ ์ถ๊ฐํ๋ค.
์ด๋, ์ค์ ๋ก DOM ๋ณ๊ฒฝ์ด ๋ฐ์ํ๋ ๊ฒ์ ํ ๋ฒ๋ฟ์ด๋ฉฐ ๋ฆฌํ๋ก์ฐ์ ๋ฆฌํ์ธํธ๋ ํ ๋ฒ๋ง ์คํํ๋ค.
๋ฐ๋ผ์, ์ฌ๋ฌ ๊ฐ์ ์์ ๋ ธ๋๋ฅผ DOM์ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ DocumentFragment ๋ ธ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์ด๋ค.
39.6.5 ๋
ธ๋ ์ฝ์
๋ง์ง๋ง ๋ ธ๋๋ก ์ถ๊ฐ
Node.prototype.appendChild ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ ธ๋๋ฅผ ์์ ์ ํธ์ถํ ๋ ธ๋์ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก DOM์ ์ถ๊ฐํ๋ค.
๋จ, ๋ ธ๋๋ฅผ ์ถ๊ฐํ ์์น๋ฅผ ์ง์ ํ ์ ์๊ณ ์ธ์ ๋ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก ์ถ๊ฐํ๋ค.
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
// ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement("li");
// ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(document.createTextNode("Orange"));
// $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
document.getElementById("fruits").appendChild($li);
</script>
</html>
์ง์ ํ ์์น์ ๋ ธ๋ ์ฝ์
Node.prototype.insertBefore(newNode, childNode) ๋ฉ์๋๋ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ ธ๋๋ฅผ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ ธ๋ ์์ ์ฝ์ ํ๋ค.
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById("fruits");
// ์์ ๋
ธ๋ ์์ฑ
const $li = document.createElement("li");
// ํ
์คํธ ๋
ธ๋๋ฅผ $li ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ก ์ถ๊ฐ
$li.appendChild(document.createTextNode("Orange"));
// $li ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์ ์์ ์์ ์ฝ์
$fruits.insertBefore($li, $fruits.lastElementChild);
// Apple - Orange - Banana
</script>
</html>
39.6.6 ๋
ธ๋ ์ด๋
DOM์ ์ด๋ฏธ ์กด์ฌํ๋ ๋ ธ๋๋ฅผ appendChild ๋๋ insertBefore ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ DOM์ ๋ค์ ์ถ๊ฐํ๋ฉด ํ์ฌ ์์น์์ ๋ ธ๋๋ฅผ ์ ๊ฑฐํ๊ณ ์๋ก์ด ์์น์ ๋ ธ๋๋ฅผ ์ถ๊ฐํ๋ค. ์ฆ, ๋ ธ๋๊ฐ ์ด๋ํ๋ค.
<html> <body> <ul id="fruits"> <li>Apple</li> <li>Banana</li> <li>Orange</li> </ul> </body> <script> const $fruits = document.getElementById("fruits"); // ์ด๋ฏธ ์กด์ฌํ๋ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ const [$apple, $banana] = $fruits.children; // ์ด๋ฏธ ์กด์ฌํ๋ $apple ์์ ๋ ธ๋๋ฅผ #fruits ์์ ๋ ธ๋์ ๋ง์ง๋ง ๋ ธ๋๋ก ์ด๋ $fruits.appendChild($apple); // Banana - Orange - Apple // ์ด๋ฏธ ์กด์ฌํ๋ $banana ์์ ๋ ธ๋๋ฅผ #fruits ์์์ ๋ง์ง๋ง ์์ ๋ ธ๋ ์์ผ๋ก ์ด๋ $fruits.insertBefore($banana, $fruits.lastElementChild); // Orange - Banana - Apple </script> </html>
39.6.7 ๋
ธ๋ ๋ณต์ฌ
Node.prototype.cloneNode([deep: true | false]) ๋ฉ์๋๋ ๋ ธ๋์ ์ฌ๋ณธ์ ์์ฑํ์ฌ ๋ฐํํ๋ค.
<html>
<body>
<ul id="fruits">
<li>Apple</li>
</ul>
</body>
<script>
const $fruits = document.getElementById("fruits");
const $apple = $fruits.firstElementChild;
// $apple ์์๋ฅผ ์์ ๋ณต์ฌํ์ฌ ์ฌ๋ณธ์ ์์ฑ. ํ
์คํธ ๋
ธ๋๊ฐ ์๋ ์ฌ๋ณธ์ด ์์ฑ๋๋ค.
const $shallowClone = $apple.cloneNode();
// ์ฌ๋ณธ ์์ ๋
ธ๋์ ํ
์คํธ ์ถ๊ฐ
$shallowClone.textContent = "Banana";
// ์ฌ๋ณธ ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($shallowClone);
// #fruits ์์๋ฅผ ๊น์ ๋ณต์ฌํ์ฌ ๋ชจ๋ ์์ ๋
ธ๋๊ฐ ํฌํจ๋ ์ฌ๋ณธ์ ์์ฑ
const $deepClone = $fruits.cloneNode(true);
// ์ฌ๋ณธ ์์ ๋
ธ๋๋ฅผ #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ๋
ธ๋๋ก ์ถ๊ฐ
$fruits.appendChild($deepClone);
</script>
</html>
39.6.8 ๋
ธ๋ ๊ต์ฒด
Node.prototype.replaceChild(newChild, oldChild) ๋ฉ์๋๋ ์์ ์ ํธ์ถํ ๋ ธ๋์ ์์ ๋ ธ๋๋ฅผ ๋ค๋ฅธ ๋ ธ๋๋ก ๊ต์ฒดํ๋ค.
<html>
<body>
<ul id="fruits">
<li>Apple</li>
</ul>
</body>
<script>
const $fruits = document.getElementById("fruits");
// ๊ธฐ์กด ๋
ธ๋์ ๊ต์ฒดํ ์์ ๋
ธ๋๋ฅผ ์์ฑ
const $newChild = document.createElement("li");
$newChild.textContent = "Banana";
// #fruits ์์ ๋
ธ๋์ ์ฒซ ๋ฒ์งธ ์์ ์์ ๋
ธ๋๋ฅผ $newChild ์์ ๋
ธ๋๋ก ๊ต์ฒด
$fruits.replaceChild($newChild, $fruits.firstElementChild);
</script>
</html>
39.6.9 ๋
ธ๋ ์ญ์
Node.prototype.removeChild(child) ๋ฉ์๋๋ child ๋งค๊ฐ๋ณ์์ ์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ฅผ DOM์์ ์ญ์ ํ๋ค.
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById("fruits");
// #fruits ์์ ๋
ธ๋์ ๋ง์ง๋ง ์์๋ฅผ DOM์์ ์ญ์
$fruits.removeChild($fruits.lastElementChild);
</script>
</html>
39.7 ์ดํธ๋ฆฌ๋ทฐํธ
39.7.1 ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋์ attributes ํ๋กํผํฐ
HTML ๋ฌธ์์ ๊ตฌ์ฑ ์์์ธ HTML ์์๋ ์ฌ๋ฌ ๊ฐ์ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ๊ฐ์ง ์ ์๋ค.
HTML ์์์ ๋์์ ์ ์ดํ๊ธฐ ์ํ ์ถ๊ฐ์ ์ธ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ค.
๋๋ถ๋ถ์ ์ดํธ๋ฆฌ๋ทฐํธ๋ ๋ชจ๋ HTML ์์์์ ์ฌ์ฉ๋ ์ ์์ง๋ง, type, value, checked ์ดํธ๋ฆฌ๋ทฐํธ๋ input ์์์๋ง ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ๋ ์๋ค.
HTML ๋ฌธ์๊ฐ ํ์ฑ๋ ๋ HTML ์์์ ์ดํธ๋ฆฌ๋ทฐํธ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๋ก ๋ณํ๋์ด ์์ ๋ ธ๋์ ์ฐ๊ฒฐ๋๋ค.
39.7.2 HTML ์ดํธ๋ฆฌ๋ทฐํธ ์กฐ์
HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ฐธ์กฐํ๋ ค๋ฉด Element.prototype.getAttribute(attributeName) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ , HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๋ณ๊ฒฝํ๋ ค๋ฉด Element.prototype.setAttribute(attributeName, attributeValue) ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
<html>
<body>
<input id="user" type="text" value="ungmo2" />
<script>
const $input = document.getElementById("user");
// value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋
const inputValue = $input.getAttribute("value");
console.log(inputValue); // ungmo2
// value ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๋ณ๊ฒฝ
$input.setAttribute("value", "foo");
console.log($input.getAttribute("value")); // foo
</script>
</body>
</html>
Last updated