
๋ฌธ์๊ฐ์ฒด๋ชจ๋ธ์ด๋ผ๊ณ ํ๋ฉฐ HTML ํน์ XML๋ฌธ์๋ฅผ ๊ตฌ์กฐํํ์ฌ ๊ณ์ธต์ ์ผ๋ก ํํ๋ ๊ฐ ์์๋ฅผ ๊ฐ์ฒด๋ก ํํ
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ์ฌ DOM ๊ตฌ์กฐ์ ์ ๊ทผํ๊ณ ์ด๋ฅผ ๋์ ์ผ๋ก ์กฐ์ํ์ฌ ๋ฌธ์ ๊ตฌ์กฐ, ์คํ์ผ, ๋ด์ฉ ๋ฑ์ ์ถ๊ฐ, ์ญ์ ํ๊ฑฐ๋ ๋ณ๊ฒฝ ํ ์ ์์
DOM์ ํธ๋ฆฌ ๊ตฌ์กฐ(๊ณ์ธต ๊ตฌ์กฐ)๋ก ์ด๋ฃจ์ด์ ธ ์์ผ๋ฉฐ, ํธ๋ฆฌ๋ฅผ ๊ตฌ์ฑํ๋ ๊ฐ ๊ฐ์ฒด๋ค์ '๋
ธ๋(Node)'๋ผ๊ณ ๋ถ๋ฆ
๋
ธ๋๋ค ์ฌ์ด์๋ ๋ถ๋ชจ(parent)์ ์์(child)๋ผ๋ ๊ณ์ธต๊ด๊ณ๊ฐ ์ ์๋จ
ex) parentNode, childNodes, firstChild, lastChild

์ถ์ฒ: https://poiemaweb.com/js-dom
<!DOCTYPE html>
<html>
<body>
<div>
<h1>Color</h1>
<ul>
<li id="red" class="box">red</li>
<li id="yellow" class="box">yellow</li>
<li id="green" class="box">green</li>
<li id="blue" class="box">blue</li>
</ul>
</div>
</body>
</html>
๋ฌธ์ ๋
ธ๋(Document Node)
document ๊ฐ์ฒด๋ฅผ ๋งํ๋ฉฐ DOM ํธ๋ฆฌ์ ์ ๊ทผํ๊ธฐ ์ํ ์์์ ์ด์ ์ต์์ ๋
ธ๋
์์ ๋
ธ๋(Element Node)
HTML ๊ตฌ์ฑ ์์ ์ฆ ํ๊ทธ๋ฅผ ์๋ฏธํ๋ฉฐ ๊ฐ๊ฐ์ Element Node๋ ๋ค์ Attribute Node์ Text Node๋ฅผ ๊ฐ์ง ์ ์์
์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋(Attribute Node)
ํ๊ทธ๋ค์ ์์ฑ์ ๊ฐ์ฒดํํ ๋
ธ๋๋ก ๋ง์ผ ํน์ ํ๊ทธ์ ์์ฑ์ ์ ๊ทผํ๋ ค๋ฉด Attribute Node๋ฅผ ์ฌ์ฉํด์ผ ํจ
ํ
์คํธ ๋
ธ๋(Text Node)
ํ๊ทธ์ ํ
์คํธ๋ฅผ ๊ฐ์ฒดํํ DOM ํธ๋ฆฌ๊ตฌ์กฐ์ ์ต์ข
๋จ ๋
ธ๋๋ก ์์ ๋
ธ๋๋ฅผ ๊ฐ์ง ์ ์์
1) querySelector(cssSelector)
- CSS ์ ํ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ ธ๋๋ฅผ ํ ๊ฐ ์ ํ
- ๋ณต์๊ฐ๊ฐ ์ ํ๋ ๊ฒฝ์ฐ, ์ฒซ๋ฒ์งธ ์์๋ ธ๋๋ง ๋ฐํ
- ์ฝค๋ง(,)๋ก ๊ตฌ๋ถ๋ ์ฌ๋ฌ ์ ๋ ํฐ๋ฅผ ๋์ด ํ ์ ์์
const red = document.querySelector('#red');
console.log(red); // li#red.box
2) querySelectorAll(selector)
- CSS ์ ํ์๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ์ ํ
- NodeList๋ฅผ ๋ฐํ
const box = document.querySelectorAll(".box");
console.log(box);
// NodeList(4)
[li#red.box, li#yellow.box, li#green.box, li#blue.box]
3) getElementById(id)
- id ์์ฑ๊ฐ์ ์ฌ์ฉํ์ฌ ์์ ๋ ธ๋๋ฅผ ํ ๊ฐ ์ ํ
- ๋ณต์๊ฐ๊ฐ ์ ํ๋ ๊ฒฝ์ฐ, ์ฒซ๋ฒ์งธ ์์๋ง ๋ฐํ
const blue = document.getElementById('blue');
console.log(blue); // li#blue.box
4) getElementsByClassName(class)
- class ์์ฑ๊ฐ์ ์ฌ์ฉํ์ฌ ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ์ ํ
- ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ฌ๋ฌ ๊ฐ์ class๋ฅผ ์ง์ ํ ์ ์์
- HTML Collection์ ๋ฐํ
const all_box = document.getElementsByClassName("box");
console.log(all_box);
// HTMLCollection(4)
[li#red.box, li#yellow.box, li#green.box, li#blue.box,
red: li#red.box, yellow: li#yellow.box, green: li#green.box, blue: li#blue.box]
5) getElementsByTagName(tagName)
- ํ๊ทธ๋ช ์ ์ฌ์ฉํ์ฌ ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ์ ํ
- HTML Collection์ ๋ฐํ
const Li = document.getElementsByTagName('li');
console.log(Li);
// HTMLCollection(4)
[li#red.box, li#yellow.box, li#green.box, li#blue.box, red: li#red.box, yellow: li#yellow.box, green: li#green.box, blue: li#blue.box]
const element = document.querySelector('ul');
1) parentNode
- ๋ถ๋ชจ ๋ ธ๋๋ฅผ ํ์
- parentElement๋ฅผ ์ฌ์ฉํ๋ฉด ์์ ํ์ ๋ถ๋ชจ ๋ ธ๋๋ง ํ์
const parentNode = element.parentNode;
console.log(parentNode); // div
2) firstChild, lastChild
- ํ ์คํธ ๋ ธ๋๋ฅผ ํฌํจํ ๋ชจ๋ ์์ ๋ ธ๋ ์ค์์ ์ฒซ๋ฒ์งธ ์์๊ณผ ๋ง์ง๋ง ์์ ๋ ธ๋๋ฅผ ํ์
- firstElementChild, lastElementChild๋ฅผ ์ฌ์ฉํ๋ฉด ์์ ๋ ธ๋ ์ค์์ ์์ ํ์ ๋ ธ๋๋ง์ ํ์
const firstChild = element.firstChild;
const lastChild = element.lastChild;
console.log(firstChild); // #text
console.log(lastChild); // #text
const firstelemChild = element.firstElementChild;
const lastelemChild = element.lastElementChild;
console.log(firstelemChild); // li#red.box
console.log(lastelemChild); // li#blue.box
3) childNodes
- ํ ์คํธ ๋ ธ๋๋ฅผ ํฌํจํ ๋ชจ๋ ์์ ๋ ธ๋๋ฅผ ํ์
- ์์ ๋ ธ๋์ ์ปฌ๋ ์ NodeList๋ฅผ ๋ฐํ
- children์ ์ฌ์ฉํ๋ฉด ์์ ๋ ธ๋ ์ค์์ ์์ ํ์ ๋ ธ๋๋ง ํ์ํ๊ณ ์์ ๋ ธ๋์ ์ปฌ๋ ์ HTMLCollection์ ๋ฐํ
const childNodes = element.childNodes;
console.log(childNodes);
// NodeList(9)
[text, li#red.box, text, li#yellow.box, text, li#green.box, text, li#blue.box, text]
const children = element.children;
console.log(children);
// HTMLCollection(4)
[li#red.box, li#yellow.box, li#green.box, li#blue.box,
red: li#red.box, yellow: li#yellow.box, green: li#green.box, blue: li#blue.box]
4) previousSibling, nextSibling
- ํ ์คํธ ๋ ธ๋๋ฅผ ํฌํจํ ๋ชจ๋ ํ์ ๋ ธ๋ ์ค์์ ์ด์ ํ์ ์ ๋ค์ ํ์ ๋ ธ๋๋ฅผ ํ์
- previousElementSibling, nextElementSibling๋ฅผ ์ฌ์ฉํ๋ฉด ํ์ ๋ ธ๋ ์ค์์ ์์ ํ์ ๋ ธ๋๋ง์ ํ์
const nextSibling = firstChild.nextSibling;
const preSibling = lastChild.previousSibling;
console.log(nextSibling); // li#red.box
console.log(preSibling); // li#blue.box
const nextElemSibling = firstelemChild.nextElementSibling;
const preElemSibling = lastelemChild.previousElementSibling;
console.log(nextElemSibling); // li#yellow.box
console.log(preElemSibling); // li#green.box
1) createElement(tagName)ํ๊ทธ์ด๋ฆ์ ์ธ์๋ก ์ ๋ฌํ์ฌ ์์๋ฅผ ์์ฑ
const newDiv = document.createElement('div');
const newUl = document.createElement('ul');
const newLi = document.createElement('li');
2) createTextNode(text)ํ ์คํธ๋ฅผ ์ธ์๋ก ์ ๋ฌํ์ฌ ํ ์คํธ ๋ ธ๋๋ฅผ ์์ฑ
const newText = document.createTextNode('content');
3) appendChild(Node)์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ฅผ ๋ง์ง๋ง ์์ ์์๋ก DOM ํธ๋ฆฌ์ ์ถ๊ฐ
document.body.appendChild(newDiv);
newDiv.appendChild(newUl);
newUl.appendChild(newLi);
newLi.appendChild(newText);
<body>
<div>
<ul>
<li>"content"</li>
</ul>
</div>
</body>
4) removeChild(Node)์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ฅผ DOM ํธ๋ฆฌ์์ ์ ๊ฑฐ
newDiv.removeChild(newUl);
// newDiv์ ์์์ธ newUl์์๋ฅผ DOM ํธ๋ฆฌ์์ ์ ๊ฑฐ
<body>
<input type="password" placeholder="๋น๋ฐ๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.">
<button type="button" onclick="show()">๋น๋ฐ๋ฒํธ ๋ณด๊ธฐ</button>
<button type="button" onclick="disable()">ํ์ฑํ/๋นํ์ฑํ</button>
</body>
<script>
const Input = document.querySelector('input');
const Btn = document.querySelector('button');
</script>
1) hasAttribute(attribute)
- ์์๊ฐ ์ง์ ํ ์์ฑ์ ๊ฐ์ง๊ณ ์๋์ง ๊ฒ์ฌ
- ์์ฑ์ ์กด์ฌ ์ ๋ฌด์ ๋ฐ๋ผ Boolean ๊ฐ์ ๋ฐํ(true / false)
console.log(Input.hasAttribute('placeholder')); //true
console.log(Input.hasAttribute('name')); //false
console.log(Btn.hasAttribute('type')); //true
2) getAttribute(attribute)
- ์์์์ ์ง์ ํ ์์ฑ์ ๊ฐ์ ์ทจ๋
- ์์ฑ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ์์ฑ๊ฐ์ ๋ฌธ์์ด๋ก ๋ฐํํ๊ณ , ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ์๋ null ๋ฐํ
console.log(Input.getAttribute('type')); //password
console.log(Input.getAttribute('value')); //null
console.log(Btn.getAttribute('type')); //button
3) setAttribute(attribute, value)
- ์์์ ์์ฑ๊ณผ ์์ฑ๊ฐ์ ์ค์
- ์์ฑ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ์์ฑ๊ฐ์ด ์ ๋ฐ์ดํธ๋๊ณ , ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ์๋ ์ง์ ๋ ์ด๋ฆ๊ณผ ๊ฐ์ผ๋ก ์ ์์ฑ์ด ์ถ๊ฐ
function show() {
Input.setAttribute('type', 'text');
// input ํ๊ทธ์ type ์์ฑ์ ๊ฐ์ password์์ text๋ก ์ค์
}
4) removeAttribute(attribute)
- ์์์์ ์ง์ ํ ์์ฑ์ ์ ๊ฑฐ
Input.removeAttribute('placeholder');
// input ํ๊ทธ์ type ์์ฑ์ ์ ๊ฑฐ
5) toggleAttribute(attribute)
- ์์์ ์์ฑ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ ์ ๊ฑฐํ๊ณ , ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ์๋ ์ถ๊ฐ
function disable() {
Input.toggleAttribute('disabled');
// input ํ๊ทธ์ disabled ์์ฑ์ ์ถ๊ฐ / ์ ๊ฑฐ
}
6) classList
- class ์์ฑ์ ๊ฐ์ ์ทจ๋ ๋๋ ๋ณ๊ฒฝ
- add, remove, toggle, contains, replace ๋ฉ์๋๋ฅผ ์ ๊ณต
<style>
.class_0 {font-weight: 400;}
.class_1 {color: red;}
.class_2 {font-size: 30px;}
.class_3 {font-weight: 900;}
</style>
<body>
<p id="text" class="class_0">Lorem ipsum</p>
</body>
<script>
const Text = document.getElementById('text');
Text.classList.add('class_1');
//class ์์ฑ์ ์ถ๊ฐํ์ฌ ๊ธ์ ์ ๋ณ๊ฒฝ
Text.classList.remove('class_1');
//class ์์ฑ์ ์ ๊ฑฐํ์ฌ ๊ธ์ ์ ์ด๊ธฐํ
Text.classList.toggle('class_2');
// class ์์ฑ์ด ์กด์ฌํ๋ฉด ์ ๊ฑฐํ๊ณ ์กด์ฌํ์ง ์์ผ๋ฉด ์ถ๊ฐํ์ฌ ๊ธ์ ํฌ๊ธฐ ๋ณ๊ฒฝ
console.log(Text.classList.contains('class_3'));
// class ์์ฑ์ ๊ฐ์ง๊ณ ์๋์ง ๊ฒ์ฌ -> false
Text.classList.replace('class_0', 'class_3');
// class ์์ฑ์ ๊ต์ฒดํ์ฌ ๊ธ์ ๋๊ป ๋ณ๊ฒฝ
</script>
<body>
<div>
<h1 id="title">This is the Title</h1>
<ul id="list">
<li>textContent</li>
<li>textContent</li>
<li>textContent</li>
</ul>
</div>
</body>
<script>
const Title = document.getElementById('title');
const List = document.getElementById('list');
</script>
1)
innerText์์์ ํ ์คํธ ์ฝํ ์ธ ์๋ง ์ ๊ทผ ๋ฐ ์กฐ์
console.log(Title.innerText); // This is the Title
Title.innerText = "<div style='color:red;'>JavaScript</div>";
// -> ํ
์คํธ๋ฅผ ๊ทธ๋๋ก ์ถ๋ ฅ
2)
innerHTML
- ํด๋น ์์์ ๋ชจ๋ ์์ ์์๋ฅผ ํฌํจํ๋ ์ฝํ ์ธ ๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ์ทจ๋
- ๋ฌธ์์ด์ ๋งํฌ์ ์ ํฌํจ
console.log(List.innerHTML);
/* <li>textContent</li>
<li>textContent</li>
<li>textContent</li> */
Title.innerHTML = "<div style='color:red;'>JavaScript</div>";
// -> ์คํ์ผ์ด ์ ์ฉ๋ ์ํ๋ก ์ถ๋ ฅ
<body>
<p id="text">Lorem ipsum</p>
<button onclick="change()">๊ธ์ ์, ํฌ๊ธฐ ๋ณ๊ฒฝ</button>
<button onclick="reset()">์ด๊ธฐํ</button>
</body>
style
- inline ์คํ์ผ ์ ์ธ์ ์์ฑ
- '-'์ผ๋ก ๊ตฌ๋ถ๋๋ ์์ฑ ์ด๋ฆ์ ์นด๋ฉ์ผ์ด์ค๋ก ๋ณํํ์ฌ ์ฌ์ฉ
function change() {
const Text = document.getElementById('text');
Text.style.fontSize = '30px';
Text.style.color = 'blue';
}
function reset() {
const Text = document.getElementById('text');
Text.style.fontSize = '16px';
Text.style.color = 'black';
}