1.1 DOM์ ์ฃผ์ ๊ฐ๋
๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ (DOM)์ ์น ๋ฌธ์์ ๋ํ ํ๋ก๊ทธ๋๋ฐ ์ธํฐํ์ด์ค์ผ. ๋ฌธ์ ๊ตฌ์กฐ๋ฅผ ๋ ธ๋ ํธ๋ฆฌ๋ก ๋ํ๋ด๋ฉฐ, ๊ฐ ๋ ธ๋๋ ๋ฌธ์์ ์ผ๋ถ๋ก, ์์, ํ ์คํธ, ์ฃผ์ ๋ฑ์ผ๋ก ๊ตฌ์ฑ๋ผ. DOM๊ณผ ๊ทธ ๋ ธ๋๋ฅผ ์ดํดํ๋ ๊ฒ์ ์น ๊ธฐ์ ์ ๋ค๋ฃจ๋ ๋ฐ ์์ด ํ์์ผ.
DOM์ ์ฃผ์ ๊ฐ๋ ๋ค
DOM์ด๋?
DOM (Document Object Model)์ HTML ๋ฌธ์์ ๋ด์ฉ์ ์ค๋ช ํ๋ ๊ณ์ธต ๊ตฌ์กฐ์ผ. ์ ํ๋ฆฌ์ผ์ด์ ๊ณผ ์คํฌ๋ฆฝํธ๊ฐ ๋ฌธ์ ๋ด์ฉ์ ๋์ ์ผ๋ก ์ ๊ทผํ๊ณ ๊ตฌ์กฐ, ์คํ์ผ, ๋ด์ฉ์ ๋ณ๊ฒฝํ ์ ์๊ฒ ํด์ค.
๋ ธ๋์ ์์
DOM ๋งฅ๋ฝ์์ ๋ฌธ์์ ๋ชจ๋ ๋ถ๋ถ์ ๋ ธ๋ (node)๋ก ๊ฐ์ฃผ๋ผ. ๋ ธ๋๋ ๋ช ๊ฐ์ง ์ ํ์ด ์์ด:
- ์์ ๋ ธ๋: HTML ํ๊ทธ๋ฅผ ๋ํ๋.
- ํ ์คํธ ๋ ธ๋: ์์ ๋ ธ๋์ ํฌํจ๋ ํ ์คํธ๋ฅผ ๋ํ๋.
- ์ฃผ์ ๋ ธ๋: HTML ์ฃผ์์ ๋ํ๋.
- ๋ฌธ์ ๋ ธ๋: ์ ์ฒด ๋ฌธ์๋ฅผ ๋ํ๋.
๋ ธ๋ ํธ๋ฆฌ
DOM์ ํธ๋ฆฌ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ฉฐ ๊ฐ ๋ ธ๋๋ ์์ ๋ ธ๋๋ค์ ๊ฐ์ง ์ ์์ด. ๋ฃจํธ ๋ ธ๋๋ ์ ์ฒด HTML ๋ฌธ์๋ฅผ ๋ํ๋ด๋ document ๊ฐ์ฒด์ผ.
DOM ๊ตฌ์กฐ ์์:
<!DOCTYPE html>
<html>
<head>
<title>Document Title</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph.</p>
</body>
</html>
์ด ๋ฌธ์์ DOM ํธ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ์ด ๋ณด์ผ ๊ฑฐ์ผ:
document
โโโ html
โโโ head
โ โโโ title
โ โโโ #text: "Document Title"
โโโ body
โโโ h1
โ โโโ #text: "Hello, World!"
โโโ p
โโโ #text: "This is a paragraph."
1.2 ๋ ธ๋์ ์ ํ
1. ์์ ๋ ธ๋
์์ ๋
ธ๋๋ HTML ํ๊ทธ์ ํด๋นํด. ์๋ฅผ ๋ค์ด, <body>
ํ๊ทธ์ ๋
ธ๋๋ ์์ ๋
ธ๋๋ผ๊ณ ๋ถ๋ฌ.
์์:
์ด ์์์์ <body>
์ <h1>
๋ ์์ ๋
ธ๋์ผ.
<body>
<h1>Hello, World!</h1>
</body>
2. ํ ์คํธ ๋ ธ๋
ํ ์คํธ ๋ ธ๋๋ ์์ ๋ ธ๋ ๋ด๋ถ์ ํ ์คํธ๋ฅผ ํฌํจํด. ํ ์คํธ ๋ ธ๋๋ ์์ ๋ ธ๋์ ์์ ๋ ธ๋๋ก ์กด์ฌํ๋ฉฐ ๋ฌธ์์ด ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๊ณ ์์ด.
์์:
์ด ์์์์ ํ
์คํธ "Hello, World!"๋ <h1>
๋
ธ๋์ ์์์ธ ํ
์คํธ ๋
ธ๋์ผ.
<h1>Hello, World!</h1>
3. ์ฃผ์ ๋ ธ๋
์ฃผ์ ๋
ธ๋๋ HTML ์ฃผ์์ ๋ํ๋ด๋ฉฐ, <!--
๋ก ์์ํด์ -->
๋ก ๋๋.
์์:
์ด ์์์์ <!-- This is a comment -->
๋ ์ฃผ์ ๋
ธ๋์ผ.
<!-- This is a comment -->
<p>This is a paragraph.</p>
4. ๋ฌธ์ ๋ ธ๋
๋ฌธ์ ๋ ธ๋๋ DOM ํธ๋ฆฌ์ ๋ฃจํธ ๋ ธ๋์ด๋ฉฐ, ๋ฌธ์์ ๋ค๋ฅธ ๋ชจ๋ ๋ ธ๋์ ๋ถ๋ชจ ๋ ธ๋์ผ.
์์:
JavaScript์ document
๊ฐ์ฒด๋ ๋ฌธ์ ๋
ธ๋๋ฅผ ๋ํ๋ด:
console.log(document);
1.3 DOM ๋ ธ๋์์ ์ํธ์์ฉ
1. ๋ ธ๋ ์ ๋ณด ์ป๊ธฐ
๋ ธ๋ ๊ฐ์ฒด์ ์์ฑ์ ํตํด ํน์ ๋ ธ๋์ ๋ํ ์ ๋ณด๋ฅผ ์ป์ ์ ์์ด. ์๋ฅผ ๋ค์ด, ๋ ธ๋์ ์ ํ, ์ด๋ฆ, ๋ด์ฉ์ ์ ์ ์์ง.
์์:
<html>
<head>
<title>๋ด ์น์ฌ์ดํธ</title>
</head>
<body>
<h1>์๋
, ์ธ์!</h1>
</body>
</html>
const heading = document.querySelector('h1');
console.log(heading.nodeName); // ์ถ๋ ฅ: H1
console.log(heading.nodeType); // ์ถ๋ ฅ: 1 (์์ ๋
ธ๋)
console.log(heading.textContent); // ์ถ๋ ฅ: "์๋
, ์ธ์!"
2. ๋ ธ๋ ๋ด์ฉ ์์
DOM ์์ฑ๊ณผ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ ธ๋์ ๋ด์ฉ์ ๋ณ๊ฒฝํ ์ ์์ด.
์์:
const paragraph = document.querySelector('p');
paragraph.textContent = 'Updated text content';
3. ์์ ๋ ธ๋ ๋ค๋ฃจ๊ธฐ
DOM์ ๊ฐ ๋ ธ๋๋ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ง ์ ์์ผ๋ฉฐ, ์์ ๋ ธ๋์ ์ ๊ทผํ๊ณ ์์ ํ ์ ์์ด.
์์:
<html>
<head>
<title>๋ด ์น์ฌ์ดํธ</title>
</head>
<body>
<ul>
<li>ํ์ฌ ์๊ฐ</li>
<li>์ฑ์ฉ ์ ๋ณด</li>
<li>์ฐ๋ฝ์ฒ</li>
</ul>
</body>
</html>
const list = document.querySelector('ul');
const firstItem = list.firstElementChild;
const lastItem = list.lastElementChild;
console.log(firstItem.textContent); // ์ฒซ ๋ฒ์งธ ๋ฆฌ์คํธ ํญ๋ชฉ์ ํ
์คํธ ์ถ๋ ฅ
console.log(lastItem.textContent); // ๋ง์ง๋ง ๋ฆฌ์คํธ ํญ๋ชฉ์ ํ
์คํธ ์ถ๋ ฅ
1.4 ์ ์ฉํ ์์ฑ๋ค
1. ์์ฑ nodeName๊ณผ nodeType
nodeName
์ ๋ ธ๋ ์ด๋ฆ์ ๋ฐํํดnodeType
์ ๋ ธ๋ ์ ํ์ ๋ฐํํด (์: 1์ ์์ ๋ ธ๋, 3์ ํ ์คํธ ๋ ธ๋)
์์:
<html>
<head>
<title>๋ด ์น์ฌ์ดํธ</title>
</head>
<body>
<h1>์๋
, ์ธ์!</h1>
</body>
</html>
const element = document.querySelector('h1');
console.log(element.nodeName); // ์ถ๋ ฅ: H1
console.log(element.nodeType); // ์ถ๋ ฅ: 1
2. ์์ฑ textContent์ innerHTML
textContent
๋ ๋ ธ๋์ ํ ์คํธ ์ฝํ ์ธ ๋ฅผ ๋ฐํํ๊ฑฐ๋ ์ค์ ํดinnerHTML
์ ๋ ธ๋์ HTML ์ฝํ ์ธ ๋ฅผ ๋ฐํํ๊ฑฐ๋ ์ค์ ํด
์์:
<html>
<head>
<title>๋ด ์น์ฌ์ดํธ</title>
</head>
<body>
<p>์ ๋ง <b>ํฅ๋ฏธ๋กญ๊ณ </b> <b>์ ์ตํ</b> ๋ฌธ๋จ!</p>
</body>
</html>
const element = document.querySelector('p');
console.log(element.textContent); // ๋ฌธ๋จ์ ํ
์คํธ ์ฝํ
์ธ ์ถ๋ ฅ
console.log(element.innerHTML); // ๋ฌธ๋จ์ HTML ์ฝํ
์ธ ์ถ๋ ฅ
3. ์์ฑ parentNode, childNodes, firstChild์ lastChild
parentNode
๋ ๋ถ๋ชจ ๋ ธ๋๋ฅผ ๋ฐํํดchildNodes
๋ ๋ชจ๋ ์์ ๋ ธ๋์ ์ปฌ๋ ์ ์ ๋ฐํํดfirstChild
๋ ์ฒซ ๋ฒ์งธ ์์ ๋ ธ๋๋ฅผ ๋ฐํํดlastChild
๋ ๋ง์ง๋ง ์์ ๋ ธ๋๋ฅผ ๋ฐํํด
์์:
<html>
<head>
<title>๋ด ์น์ฌ์ดํธ</title>
</head>
<body>
<ul>
<li>ํ์ฌ ์๊ฐ</li>
<li>์ฑ์ฉ ์ ๋ณด</li>
<li>์ฐ๋ฝ์ฒ</li>
</ul>
</body>
</html>
const element = document.querySelector('ul');
console.log(element.parentNode); // ๋ถ๋ชจ ๋
ธ๋ ์ถ๋ ฅ
console.log(element.childNodes); // ์์ ๋
ธ๋ ์ปฌ๋ ์
์ถ๋ ฅ
console.log(element.firstChild); // ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋ ์ถ๋ ฅ
console.log(element.lastChild); // ๋ง์ง๋ง ์์ ๋
ธ๋ ์ถ๋ ฅ
GO TO FULL VERSION