ํฐ์คํ ๋ฆฌ ๋ทฐ
๋ธ๋ผ์ฐ์ ์ ๋ ๋๋ง ์์ง์ HTML ๋ฌธ์๋ฅผ ํ์ฑํ์ฌ ๋ธ๋ผ์ฐ์ ๊ฐ ์ดํดํ ์ ์๋ ์๋ฃ๊ตฌ์กฐ์ธ DOM์ ์์ฑํฉ๋๋ค. ์ด๋ ์์ฑ๋๋ DOM(Document Object Model)์ HTML ๋ฌธ์์ ๊ณ์ธต์ ๊ตฌ์กฐ์ ์ ๋ณด๋ฅผ ํํํ๋ฉฐ ์ด๋ฅผ ์ ์ดํ ์ ์๋ API(ํ๋กํผํฐ์ ๋ฉ์๋)๋ฅผ ์ ๊ณตํ๋ ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ์ ๋๋ค.
๋ ธ๋
HTML ์์์ ๋ ธ๋ ๊ฐ์ฒด
HTML ์์๋ HTML ๋ฌธ์๋ฅผ ๊ตฌ์ฑํ๋ ๊ฐ๋ณ์ ์ธ ์์๋ฅผ ์๋ฏธํฉ๋๋ค.

์ด๋ฌํ HTML ์์๋ ๋ ๋๋ง ์์ง์ ์ํด ํ์ฑ๋์ด DOM์ ๊ตฌ์ฑํ๋ ์์ ๋ ธ๋ ๊ฐ์ฒด๋ก ๋ณํ๋ฉ๋๋ค. ์ด๋, HTML ์์์ ์ดํธ๋ฆฌ๋ทฐํธ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๋ก, HTML ์์์ ํ ์คํธ ์ฝํ ์ธ ๋ ํ ์คํธ ๋ ธ๋๋ก ๋ณํ๋ฉ๋๋ค.

HTML ๋ฌธ์๋ ์ค์ฒฉ ๊ด๊ณ๋ฅผ ๊ฐ์ง๋ HTML ์์๋ค์ ์งํฉ(ํ ์คํธ ๋ฟ๋ง ์๋๋ผ ๋ค๋ฅธ HTML ์์๋ ํฌํจ)์ผ๋ก ์ด๋ฃจ์ด์ ธ ์์ด ๊ณ์ธต์ ์ธ ๋ถ์ ๊ด๊ณ๊ฐ ํ์ฑ๋ฉ๋๋ค.
๋ฐ๋ผ์ HTML ์์๋ฅผ ๊ฐ์ฒดํํ ๋ชจ๋ ๋ ธ๋ ๊ฐ์ฒด๋ค์ ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค.

- ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ๋ ๋ถ๋ชจ ๋ ธ๋์ ์์ ๋ ธ๋๋ก ๊ตฌ์ฑ๋์ด ๋ ธ๋ ๊ฐ์ ๊ณ์ธต์ ๊ตฌ์กฐ๋ฅผ ํํํ๋ ๋น์ ํ ์๋ฃ๊ตฌ์กฐ
- ํ๋์ ์ต์์ ๋ ธ๋(๋ฃจํธ๋ ธ๋)์์ ์์ํ๋๋ฐ, ์ต์์ ๋ ธ๋๋ ๋ถ๋ชจ ๋ ธ๋๊ฐ ์์
- ๋ฃจํธ ๋ ธ๋๋ 0๊ฐ ์ด์์ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ง
- ์์ ๋ ธ๋๊ฐ ์๋ ๋ ธ๋๋ฅผ ๋ฆฌํ ๋ ธ๋๋ผ ํจ
์ด๋ ๊ฒ ๋ ธ๋ ๊ฐ์ฒด๋ค๋ก ๊ตฌ์ฑ๋ ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ๋ฅผ DOM์ด๋ผ ํ๋ฉฐ, DOM ํธ๋ฆฌ๋ผ๊ณ ๋ถ๋ฅด๊ธฐ๋ ํฉ๋๋ค.
๋ ธ๋ ๊ฐ์ฒด ํ์
๋ ธ๋ ๊ฐ์ฒด๋ ์ด 12๊ฐ์ ์ข ๋ฅ(๋ ธ๋ ํ์ )์ด ์๊ณ ์์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋๋ค. ์ด ์ค์์ ์ค์ํ ๋ ธ๋ ํ์ ์ ๋ค์๊ณผ ๊ฐ์ด 4๊ฐ์ง๊ฐ ์์ต๋๋ค.
๋ ธ๋ ํ์ | ์ค๋ช |
๋ฌธ์ ๋ ธ๋ | - DOM ํธ๋ฆฌ์ ์ต์์์ ์กด์ฌํ๋ ๋ฃจํธ ๋
ธ๋๋ก์ *document ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด - window.document ๋๋ document๋ก ์ฐธ์กฐ ๊ฐ๋ฅ - HTML ๋ฌธ์๋น document ๊ฐ์ฒด๋ ์ ์ผ - DOM ํธ๋ฆฌ์ ๋ ธ๋๋ค์ ์ ๊ทผํ๊ธฐ ์ํ ์ ์ ์ ์ญํ ์ ๋ด๋น |
์์ ๋ ธ๋ | - HTML ์์๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด - ๋ฌธ์์ ๊ตฌ์กฐ๋ฅผ ํํํจ - HTML ์์ ๊ฐ์ ์ค์ฒฉ์ ์ํด ๋ถ์ ๊ด๊ณ๋ฅผ ๊ฐ์ง๋ฉฐ, ์ด๋ฅผ ํตํด ์ ๋ณด๋ฅผ ๊ตฌ์กฐํํจ |
์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋ | - HTML ์์์ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด - ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์ง์ ๋ HTML ์์์ ์์ ๋ ธ๋์ ์ฐ๊ฒฐ๋์ด ์์ - ๋ถ๋ชจ ๋ ธ๋๊ฐ ์์ด ์์ ๋ ธ๋์ ํ์ ๋ ธ๋๋ ์๋ - ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋์ ์ ๊ทผํ์ฌ ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ฐธ์กฐ/๋ณ๊ฒฝ ํ๋ ค๋ฉด ๋จผ์ ์์ ๋ ธ๋์ ์ ๊ทผํด์ผ ํจ |
ํ ์คํธ ๋ ธํธ | - HTML ์์์ ํ
์คํธ๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด - ๋ฌธ์์ ์ ๋ณด๋ฅผ ํํํจ - ์์ ๋ ธ๋์ ์์ ๋ ธ๋์ด๋ฉฐ, ์์์ ๊ฐ์ง ์ ์๋ ๋ฆฌํ ๋ ธ๋์ - DOM ํธ๋ฆฌ์ ์ต์ข ๋จ์์ผ๋ก ํ ์คํธ ๋ ธ๋์ ์ ๊ทผํ๋ ค๋ฉด ๋จผ์ ๋ถ๋ชจ ๋ ธ๋์ธ ์์ ๋ ธ๋์ ์ ๊ทผํด์ผ ํจ |
*document ๊ฐ์ฒด : ๋ธ๋ผ์ฐ์ ๊ฐ ๋ ๋๋งํ HTML ๋ฌธ์ ์ ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ๊ฐ์ฒด๋ก์ ์ ์ญ ๊ฐ์ฒด window์ document ํ๋กํผํฐ์ ๋ฐ์ธ๋ฉ๋์ด ์์
๋ ธ๋ ๊ฐ์ฒด์ ์์ ๊ตฌ์กฐ
DOM์ ๊ตฌ์ฑํ๋ ๋ ธ๋ ๊ฐ์ฒด๋ ECMAScript ์ฌ์์ ์ ์๋ ํ์ค ๋นํธ์ธ ๊ฐ์ฒด๊ฐ ์๋๋ผ ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ ์ถ๊ฐ์ ์ผ๋ก ์ ๊ณตํ๋ ํธ์คํธ ๊ฐ์ฒด์ ๋๋ค. ํ์ง๋ง, ๋ ธ๋ ๊ฐ์ฒด๋ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ฒด์ด๋ฏ๋ก ํ๋กํ ํ์ ์ ์ํ ์์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋๋ค.

์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ์์ ๊ด๊ณ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ๋ชจ๋ ๋ ธ๋ ๊ฐ์ฒด : Object, EventTarget, Node ์ธํฐํ์ด์ค๋ฅผ ์์
- ๋ฌธ์ ๋ ธ๋ : Document, HTMLDocument ์ธํฐํ์ด์ค๋ฅผ ์์
- ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋ : Attr ์ธํฐํ์ด์ค๋ฅผ ์์
- ํ ์คํธ ๋ ธ๋ : CharacterData ์ธํฐํ์ด์ค๋ฅผ ์์
- ์์ ๋ ธ๋ : Element ์ธํฐํ์ด์ค์ HTMLElement์ ํ๊ทธ์ ์ข ๋ฅ๋ณ๋ก ์ธ๋ถํ๋ ์ธํฐํ์ด์ค๋ค๋ฅผ ์์๋ฐ์
์ด๋ฅผ ํ๋กํ ํ์ ์ฒด์ธ ๊ด์ ์์ ์ดํด๋ณด๋ฉด ์๋ฅผ ๋ค์ด input ์์๋ฅผ ํ์ฑํ์ฌ ๊ฐ์ฒดํํ input ์์ ๋ ธ๋ ๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ์ด ์์ ์ ํ๋กํ ํ์ ์ฒด์ธ์ ์๋ ๋ชจ๋ ํ๋กํ ํ์ ์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์์๋ฐ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.

<!DOCTYPE html>
<html>
<body>
<input type="text">
<script>
// input ์์ ๋
ธ๋ ๊ฐ์ฒด๋ฅผ ์ ํ
const $input = document.querySelector('input');
// input ์์ ๋
ธ๋ ๊ฐ์ฒด์ ํ๋กํ ํ์
์ฒด์ธ
console.log(
Object.getPrototypeOf($input) === HTMLInputElement.prototype,
Object.getPrototypeOf(HTMLInputElement.prototype) === HTMLElement.prototype,
Object.getPrototypeOf(HTMLElement.prototype) === Element.prototype,
Object.getPrototypeOf(Element.prototype) === Node.prototype,
Object.getPrototypeOf(Node.prototype) === EventTarget.prototype,
Object.getPrototypeOf(EventTarget.prototype) === Object.prototype
); // ๋ชจ๋ true
</script>
</body>
</html>
๋ฐฐ์ด์ด ๊ฐ์ฒด์ธ ๋์์ ๋ฐฐ์ด์ธ ๊ฒ์ฒ๋ผ input ์์ ๋ ธ๋ ๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ์ด ๋ค์ํ ํน์ฑ์ ๊ฐ๋ ๊ฐ์ฒด์ด๋ฉฐ, ์ด๋ฌํ ํน์ฑ์
๋ํ๋ด๋ ๊ธฐ๋ฅ๋ค์ ์์์ ํตํด ์ ๊ณต๋ฐ์ต๋๋ค.
input ์์ ๋ ธ๋ ๊ฐ์ฒด์ ํน์ฑ | ํ๋กํ ํ์ ์ ์ ๊ณตํ๋ ๊ฐ์ฒด |
๊ฐ์ฒด | Object |
์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํค๋ ๊ฐ์ฒด | EventTarget |
ํธ๋ฆฌ ์๋ฃ๊ตฌ์กฐ์ ๋ ธ๋ ๊ฐ์ฒด | Node |
๋ธ๋ผ์ฐ์ ๊ฐ ๋ ๋๋งํ ์ ์๋ ์น ๋ฌธ์์ ์์๋ฅผ ํํํ ๊ฐ์ฒด | Element |
์น ๋ฌธ์์ ์์ ์ค์์ HTML ์์๋ฅผ ํํํ๋ ๊ฐ์ฒด | HTMLElemnet |
HTML ์์ ์ค์์ input ์์๋ฅผ ํํํ๋ ๊ฐ์ฒด | HTMLInputElement |
*๋ ธ๋ ๊ฐ์ฒด์ ์์ ๊ตฌ์กฐ๋ ๊ฐ๋ฐ์ ๋๊ตฌ์ Elements ํจ๋ ์ฐ์ธก์ Properties ํจ๋์์ ํ์ธ ๊ฐ๋ฅ
์ด์ฒ๋ผ ๋ ธ๋ ๊ฐ์ฒด์๋ ๋ ธ๋ ๊ฐ์ฒด์ ํ์ ์ ์๊ด์์ด ๋ชจ๋ ๋ ธ๋ ๊ฐ์ฒด๊ฐ ๊ณตํต์ผ๋ก ๊ฐ๋ ๊ธฐ๋ฅ๋ ์๊ณ , ๋ ธ๋ ํ์ ์ ๋ฐ๋ผ ๊ณ ์ ํ ๊ธฐ๋ฅ๋ ์์ต๋๋ค. ๋ฐ๋ผ์ ํ์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ์ธํฐํ์ด์ค๊ฐ HTML ์์์ ์ข ๋ฅ์ ๋ฐ๋ผ ๊ฐ๊ฐ ๋ค๋ฆ ๋๋ค.
๋ํ, ๋ ธ๋ ๊ฐ์ฒด๋ ๊ณตํตํ๋ ๊ธฐ๋ฅ์ผ์๋ก ํ๋กํ ํ์ ์ฒด์ธ์ ์์์, ๊ฐ๋ณ์ ์ธ ๊ณ ์ ๊ธฐ๋ฅ์ผ์๋ก ํ๋กํ ํ์ ์ฒด์ธ์ ํ์์ ํ๋กํ ํ์ ์ฒด์ธ์ ๊ตฌ์ถํ์ฌ ๋ ธ๋ ๊ฐ์ฒด์ ํ์ํ ๊ธฐ๋ฅ(ํ๋กํผํฐ์ ๋ฉ์๋)๋ฅผ ์ ๊ณตํ๋ ์์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋๋ค.
์ ๋ฆฌํ์๋ฉด DOM์ HTML ๋ฌธ์์ ๊ณ์ธต์ ๊ตฌ์กฐ์ ์ ๋ณด๋ฅผ ํํํ๋ ๊ฒ์ ๋ฌผ๋ก ๋ ธ๋ ๊ฐ์ฒด์ ์ข ๋ฅ(ํ์ )์ ๋ฐ๋ผ ํ์ํ ๊ธฐ๋ฅ์ ํ๋กํผํฐ์ ๋ฉ์๋์ ์งํฉ์ธ DOM API๋ก ์ ๊ณตํฉ๋๋ค. ์ด๋ฌํ DOM API๋ฅผ ํตํด HTML ๊ตฌ์กฐ๋ ๋ด์ฉ ๋๋ ์คํ์ผ ๋ฑ์ ๋์ ์ผ๋ก ์กฐ์ํ ์ ์์ต๋๋ค.
์์ ๋ ธ๋ ์ทจ๋
HTML์ ๊ตฌ์กฐ๋ ๋ด์ฉ ๋๋ ์คํ์ผ ๋ฑ์ ๋์ ์ผ๋ก ์กฐ์ํ๋ ค๋ฉด ๋จผ์ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํด์ผ ํฉ๋๋ค.
- ํ ์คํธ ๋ ธ๋ : ์์ ๋ ธ๋์ ์์
- ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋ : ์์ ๋ ธ๋์ ์ฐ๊ฒฐ๋์ด ์์
์ด์ฒ๋ผ HTML ์์๋ฅผ ์กฐ์ํ๋ ์์์ ์ธ ์์ ๋ ธ๋์ ์ทจ๋์ ์ํด DOM์ ๋ค์ํ ๋ฉ์๋๋ฅผ ์ ๊ณตํฉ๋๋ค.
id๋ฅผ ์ด์ฉํ ์์ ๋ ธ๋ ์ทจ๋
Document.prototype.getElementById ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌํ id ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๊ฐ๋ ํ๋์ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ฉฐ, getElementById๋ Document.prototype์ ํ๋กํผํฐ๋ก ๋ฐ๋์ ๋ฌธ์ ๋ ธ๋์ธ document๋ฅผ ํตํด ํธ์ถํด์ผ ํฉ๋๋ค.
getElementById ๋ฉ์๋๋ ๋ค์๊ณผ ๊ฐ์ ํน์ง์ ๊ฐ์ง๊ณ ์์ต๋๋ค.
<!DOCTYPE html>
<html>
<body>
<ul>
<li id="apple">Apple</li>
<li id="banana">Banana</li>
<li id="banana">Orange</li>
<li id="orange">Orange</li>
</ul>
<script>
// getElementById ๋ฉ์๋๋ ์ธ์ ๋ ๋จ ํ๋์ ์์ ๋
ธ๋๋ฅผ ๋ฐํํ๋ค.
// id ๊ฐ์ด 'banana'์ธ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
// ๋ ๋ฒ์งธ li ์์๊ฐ ํ์ฑ๋์ด ์์ฑ๋ ์์ ๋
ธ๋๊ฐ ๋ฐํ๋๋ค.
const $elem1 = document.getElementById('banana');
// id ๊ฐ์ด 'grape'์ธ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค. null์ด ๋ฐํ๋๋ค.
const $elem2 = document.getElementById('grape');
// ์ทจ๋ํ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$elem1.style.color = 'red';
// null๊ฐ์ stype์ ์ฐพ์ ์ ์์
$elem2.style.color = 'red'; // -> TypeError: Cannot read property 'stype' of null
</script>
</body>
</html>
- id ๊ฐ์ HTML ๋ฌธ์ ๋ด์์ ์ ์ผํ ๊ฐ์ด์ด์ผ ํ๋ฉฐ, class ์ดํธ๋ฆฌ๋ทฐํธ์๋ ๋ฌ๋ฆฌ ๊ณต๋ฐฑ ๋ฌธ์๋ก ๊ตฌ๋ถํ์ฌ ์ฌ๋ฌ๊ฐ์ ๊ฐ์ ๊ฐ์ง ์ ์์(์ฌ๋ฌ ๊ฐ ์กด์ฌํ๋๋ผ๋ ์๋ฌ๋ ๋ฐ์ํ์ง ์์)
- HTML ๋ฌธ์ ๋ด์ ์ค๋๋ id ๊ฐ์ ๊ฐ๋ HTML ์์๊ฐ ์ฌ๋ฌ ๊ฐ ์กด์ฌํ๋ฉด getElementById ๋ฉ์๋๋ id ๊ฐ์ ๊ฐ๋ ์ฒซ ๋ฒ์งธ ์์ ๋ ธ๋๋ง ๋ฐํ
- ์ธ์๋ก ์ ๋ฌ๋ id ๊ฐ์ ๊ฐ๋ HTML ์์๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ getElementById ๋ฉ์๋๋ null์ ๋ฐํ
// ์๋ฌต์ ์ ์ธ๊ณผ ๋
ธ๋ ๊ฐ์ฒด ํ ๋น
<!DOCTYPE html>
<html>
<body>
<div id="foo"></div>
<script>
// id ๊ฐ๊ณผ ๋์ผํ ์ด๋ฆ์ ์ ์ญ ๋ณ์๊ฐ ์๋ฌต์ ์ผ๋ก ์ ์ธ๋๊ณ ํด๋น ๋
ธ๋ ๊ฐ์ฒด๊ฐ ํ ๋น๋๋ค.
console.log(foo === document.getElementById('foo')); // true
// ์๋ฌต์ ์ ์ญ์ผ๋ก ์์ฑ๋ ์ ์ญ ํ๋กํผํฐ๋ ์ญ์ ๋์ง๋ง ์ ์ญ ๋ณ์๋ ์ญ์ ๋์ง ์๋๋ค.
delete foo;
console.log(foo); // <div id="foo"></div>
</script>
</body>
</html>
// ์ ์ญ ๋ณ์๊ฐ ์๋ค๋ฉด ์ฌํ ๋น๋์ง ์์
<!DOCTYPE html>
<html>
<body>
<div id="foo"></div>
<script>
let foo = 1;
// id ๊ฐ๊ณผ ๋์ผํ ์ด๋ฆ์ ์ ์ญ ๋ณ์๊ฐ ์ด๋ฏธ ์ ์ธ๋์ด ์์ผ๋ฉด ๋
ธ๋ ๊ฐ์ฒด๊ฐ ์ฌํ ๋น๋์ง ์๋๋ค.
console.log(foo); // 1
</script>
</body>
</html>
- HTML ์์์ id ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ๋ถ์ฌํ๋ฉด id ๊ฐ๊ณผ ๋์ผํ ์ด๋ฆ์ ์ ์ญ ๋ณ์๊ฐ ์๋ฌต์ ์ผ๋ก ์ ์ธ๋๊ณ ํด๋น ๋ ธ๋ ๊ฐ์ฒด๊ฐ ํ ๋น๋๋ ๋ถ์ ํจ๊ณผ๊ฐ ์์
- ๋ง์ฝ, id ๊ฐ๊ณผ ๋์ผํ ์ด๋ฆ์ ์ ์ญ ๋ณ์๊ฐ ์ด๋ฏธ ์ ์ธ๋์ด ์๋ค๋ฉด ์ด ์ ์ญ ๋ณ์์ ๋ ธ๋ ๊ฐ์ฒด๊ฐ ์ฌํ ๋น๋์ง ์์
ํ๊ทธ ์ด๋ฆ์ ์ด์ฉํ ์์ ๋ ธ๋ ์ทจ๋
Document.prototype/Element.prototype.getElementsByTagName ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌํ ํ๊ทธ ์ด๋ฆ์ ๊ฐ๋ ๋ชจ๋ ์์ ๋ ธ๋๋ค์ ํ์ํ์ฌ ๋ฐํํฉ๋๋ค. ๋ฉ์๋ ์ด๋ฆ์ ํฌํจ๋ Elements๊ฐ ๋ณต์ํ์ธ ๊ฒ์์ ์ ์ ์๋ฏ์ด ์ฌ๋ฌ ๊ฐ์ ์์ ๋ ธ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ธ HTMLCollection ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
getElementsByTagName์ Document.prototype์ ์ ์๋ ๋ฉ์๋, Element.prototye์ ์ ์๋ ๋ฉ์๋๊ฐ ์์ต๋๋ค.
- Document.prototype.getElementsByTagName : DOM์ ๋ฃจํธ ๋ ธ๋(๋ฌธ์ ๋ ธ๋, document)๋ฅผ ํตํด ํธ์ถํด ์ ์ฒด ํ์
- Element.prototype.getElementsByTagName : ํน์ ์์ ๋ ธ๋๋ฅผ ํตํด ํธ์ถํด ์์ ๋ ธ๋ ํ์
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
<li>Orange</li>
</ul>
<ul>
<li>HTML</li>
</ul>
<script>
// DOM ์ ์ฒด์์ ํ๊ทธ ์ด๋ฆ์ด li์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $lisFromDocument = document.getElementsByTagName('li');
console.log($lisFromDocument); // HTMLCollection(4) [li, li, li, li]
// ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $all = document.getElementsByTagName('*');
// -> HTMLCollection(8) [html, head, body, ul, li#apple, li#banana, li#orange, script, apple: li#apple, banana: li#banana, orange: li#orange]
// #fruits ์์์ ์์ ๋
ธ๋ ์ค์์ ํ๊ทธ ์ด๋ฆ์ด li์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋
// ํ์ํ์ฌ ๋ฐํํ๋ค.
const $fruits = document.getElementById('fruits');
const $lisFromFruits = $fruits.getElementsByTagName('li');
console.log($lisFromFruits); // HTMLCollection(3) [li, li, li]
</script>
</body>
</html>
- getElementsByTagName ๊ฐ ๋ฐํํ๋ DOM ์ปฌ๋ ฅ์ผ ๊ฐ์ฒด์ธ HTMLCollection ๊ฐ์ฒด๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฉด์ ์ดํฐ๋ฌ๋ธ
- HTML ๋ฌธ์์ ๋ชจ๋ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ๋ ค๋ฉด getElementsByTagName์ ์ธ์๋ก *๋ฅผ ์ ๋ฌํด์ผ ํจ
- ๋ง์ฝ ์ธ์๋ก ์ ๋ฌ๋ ํ๊ทธ ์ด๋ฆ์ ๊ฐ๋ ์์๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋น HTMLCollection ๊ฐ์ฒด๋ฅผ ๋ฐํ
class๋ฅผ ์ด์ฉํ ์์ ๋ ธ๋ ์ทจ๋
Document.prototype/Element.prototype.getElementsByClassName ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌํ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๊ฐ๋ ๋ชจ๋ ์์ ๋ ธ๋๋ค์ ํ์ํ์ฌ ๋ฐํํฉ๋๋ค. getElementsByClassName ๋ฉ์๋ ๋ํ Document.prototype์ ์ ์๋ ๋ฉ์๋, Element.prototye์ ์ ์๋ ๋ฉ์๋๊ฐ ์์ต๋๋ค.
- Document.prototype.getElementsByClassName : DOM์ document๋ฅผ ํตํด ํธ์ถํด ์ ์ฒด ํ์
- Element.prototype.getElementsByClassName : ํน์ ์์ ๋ ธ๋๋ฅผ ํตํด ํธ์ถํด ์์ ๋ ธ๋ ํ์
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
<div class="banana">Banana</div>
<script>
// DOM ์ ์ฒด์์ class ๊ฐ์ด 'banana'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $bananasFromDocument = document.getElementsByClassName('banana');
console.log($bananasFromDocument); // HTMLCollection(2) [li.banana, div.banana]
// #fruits ์์์ ์์ ๋
ธ๋ ์ค์์ class ๊ฐ์ด 'banana'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $fruits = document.getElementById('fruits');
const $bananasFromFruits = $fruits.getElementsByClassName('banana');
console.log($bananasFromFruits); // HTMLCollection [li.banana]
// class ๊ฐ์ด 'fruit apple'์ธ ์์ ๋
ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ HTMLCollection ๊ฐ์ฒด์ ๋ด์ ๋ฐํํ๋ค.
const $apples = document.getElementsByClassName('fruit apple');
// ์ทจ๋ํ ๋ชจ๋ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
[...$apples].forEach(elem => { elem.style.color = 'blue'; });
</script>
</body>
</html>
- ์ธ์๋ก ์ ๋ฌํ class ๊ฐ์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ฌ๋ฌ ๊ฐ์ class๋ฅผ ์ง์ ํ ์ ์์
- getElementsByClassName๋ ์ฌ๋ฌ ๊ฐ์ ์์ ๋ ธ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ธ HTMLCollection๊ฐ์ฒด๋ฅผ ๋ฐํ
- ๋ง์ฝ, ์ธ์๋ก ์ ๋ฌ๋ class ๊ฐ์ ๊ฐ๋ ์์๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋น HTMLCollection๊ฐ์ฒด๋ฅผ ๋ฐํ
CSS ์ ํ์๋ฅผ ์ด์ฉํ ์์ ๋ ธ๋ ์ทจ๋
CSS ์ ํ์(selector)๋ ์คํ์ผ์ ์ ์ฉํ๊ณ ์ ํ๋ HTML ์์๋ฅผ ํน์ ํ ๋ ์ฌ์ฉํ๋ ๋ฌธ๋ฒ์ ๋๋ค.
/* ์ ์ฒด ์ ํ์: ๋ชจ๋ ์์๋ฅผ ์ ํ */
* { ... }
/* ํ๊ทธ ์ ํ์: ๋ชจ๋ p ํ๊ทธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
p { ... }
/* id ์ ํ์: id ๊ฐ์ด 'foo'์ธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
#foo { ... }
/* class ์ ํ์: class ๊ฐ์ด 'foo'์ธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
.foo { ... }
/* ์ดํธ๋ฆฌ๋ทฐํธ ์ ํ์: input ์์ ์ค์ type ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ด 'text'์ธ ์์๋ฅผ ๋ชจ๋ ์ ํ */
input[type=text] { ... }
/* ํ์ ์ ํ์: div ์์์ ํ์ ์์ ์ค p ์์๋ฅผ ๋ชจ๋ ์ ํ */
div p { ... }
/* ์์ ์ ํ์: div ์์์ ์์ ์์ ์ค p ์์๋ฅผ ๋ชจ๋ ์ ํ */
div > p { ... }
/* ์ธ์ ํ์ ์ ํ์: p ์์์ ํ์ ์์ ์ค์ p ์์ ๋ฐ๋ก ๋ค์ ์์นํ๋ ul ์์๋ฅผ ์ ํ */
p + ul { ... }
/* ์ผ๋ฐ ํ์ ์ ํ์: p ์์์ ํ์ ์์ ์ค์ p ์์ ๋ค์ ์์นํ๋ ul ์์๋ฅผ ๋ชจ๋ ์ ํ */
p ~ ul { ... }
/* ๊ฐ์ ํด๋์ค ์ ํ์: hover ์ํ์ธ a ์์๋ฅผ ๋ชจ๋ ์ ํ */
a:hover { ... }
/* ๊ฐ์ ์์ ์ ํ์: p ์์์ ์ฝํ
์ธ ์ ์์ ์์นํ๋ ๊ณต๊ฐ์ ์ ํ
์ผ๋ฐ์ ์ผ๋ก content ํ๋กํผํฐ์ ํจ๊ป ์ฌ์ฉ๋๋ค. */
p::before { ... }
Document.prototype/Element.prototype.querySelector ๋ฉ์๋
์ธ์๋ก ์ ๋ฌํ CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ํ๋์ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํฉ๋๋ค.
- ์ธ์๋ก ์ ํํ CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ์์ ๋ ธ๋๊ฐ ์ฌ๋ฌ ๊ฐ์ธ ๊ฒฝ์ฐ ์ฒซ ๋ฒ์งธ ์์ ๋ ธ๋๋ง ๋ฐํ
- ์ธ์๋ก ์ ํํ CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ์์ ๋ ธ๋๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ null ๋ฐํ
- ์ธ์๋ก ์ ํํ CSS ์ ํ์๊ฐ ๋ฌธ๋ฒ์ ๋ง์ง ์๋ ๊ฒฝ์ฐ DOMException ์๋ฌ๊ฐ ๋ฐ์
<!DOCTYPE html>
<html>
<body>
<ul>
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
<script>
// class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ด 'banana'์ธ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $elem = document.querySelector('.banana');
// ์ทจ๋ํ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$elem.style.color = 'red';
</script>
</body>
</html>
Document.prototype/Element.prototype.querySelectorAll ๋ฉ์๋
์ธ์๋ก ์ ๋ฌํ CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ๋ชจ๋ ์์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํฉ๋๋ค.
- ์ฌ๋ฌ ๊ฐ์ ์์ ๋ ธ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ธ NodeList(์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด and ์ดํฐ๋ฌ๋ธ)๊ฐ์ฒด๋ฅผ ๋ฐํ
- ์ธ์๋ก ์ ํํ CSS ์ ํ์๋ฅผ ๋ง์กฑ์ํค๋ ์์ ๋ ธ๋๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋น NodeList ๊ฐ์ฒด๋ฅผ ๋ฐํ
- ์ธ์๋ก ์ ํํ CSS ์ ํ์๊ฐ ๋ฌธ๋ฒ์ ๋ง์ง ์๋ ๊ฒฝ์ฐ DOMException ์๋ฌ๊ฐ ๋ฐ์
- HTML์ ๋ฌธ์์ ๋ชจ๋ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ๋ ค๋ฉด ์ธ์๋ก ์ ์ฒด ์ ํ์ *๋ฅผ ์ ๋ฌ
<!DOCTYPE html>
<html>
<body>
<ul>
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
<script>
// ul ์์์ ์์ ์์์ธ li ์์๋ฅผ ๋ชจ๋ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $elems = document.querySelectorAll('ul > li');
// ์ทจ๋ํ ์์ ๋
ธ๋๋ค์ NodeList ๊ฐ์ฒด์ ๋ด๊ฒจ ๋ฐํ๋๋ค.
console.log($elems); // NodeList(3) [li.apple, li.banana, li.orange]
// ์ทจ๋ํ ๋ชจ๋ ์์ ๋
ธ๋์ style.color ํ๋กํผํฐ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
// NodeList๋ forEach ๋ฉ์๋๋ฅผ ์ ๊ณตํ๋ค.
$elems.forEach(elem => { elem.style.color = 'red'; });
// ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํํ๋ค.
const $all = document.querySelectorAll('*');
// -> NodeList(8) [html, head, body, ul, li#apple, li#banana, li#orange, script]
</script>
</body>
</html>
querySelector ์ querySelectorAll ๋ฉ์๋ ๋ํ document๋ฅผ ํตํด ํธ์ถํด DOM ์ ์ฒด๋ฅผ ํ์ํ๋ ๋ฉ์๋์ ํน์ ์์์ ์์ ๋ ธ๋๋ฅผ ํ์ํ๋ ๋ฉ์๋๊ฐ Document.prototype๊ณผ Element.prototype์ ๊ฐ๊ฐ ์ ์๋์ด ์์ต๋๋ค.
์ด๋ฌํ CSS ์ ํ์ ๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ ์ฅ๋จ์ ์ด ์กด์ฌํฉ๋๋ค.
- querySelector ์ querySelectorAll ๋ฉ์๋๋ ๋ค๋ฅธ ์์ ์ทจ๋ ๋ฉ์๋๋ค์ ๋นํด ๋ค์ ๋๋ฆฐ ๊ฒ์ผ๋ก ์๋ ค์ ธ ์์(๋จ์ )
- CSS ์ ํ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ข ๋ ๊ตฌ์ฒด์ ์ธ ์กฐ๊ฑด์ผ๋ก ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ ์ ์์(์ฅ์ )
๋ฐ๋ผ์ id ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์๋ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ๋ ๊ฒฝ์ฐ getElementById ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ณ , ๊ทธ ์ด์ธ์ ๊ฒฝ์ฐ์๋ querySelector ์ querySelectorAll ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ถ์ฅ๋ฉ๋๋ค.
ํน์ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ ์ ์๋์ง ํ์ธ
Element.prototype.matches ๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌํ CSS ์ ํ์๋ฅผ ํตํด ํน์ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ ์ ์๋์ง ํ์ธํฉ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
const $apple = document.querySelector('.apple');
// $apple ๋
ธ๋๋ '#fruits > li.apple'๋ก ์ทจ๋ํ ์ ์๋ค.
console.log($apple.matches('#fruits > li.apple')); // true
// $apple ๋
ธ๋๋ '#fruits > li.banana'๋ก ์ทจ๋ํ ์ ์๋ค.
console.log($apple.matches('#fruits > li.banana')); // false
</script>
</html>
HTMLCollection๊ณผ NodeList
DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ธ HTMLCollection๊ณผ NodeList๋ DOM API๊ฐ ์ฌ๋ฌ ๊ฐ์ ๊ฒฐ๊ณผ๊ฐ์ ๋ฐํํ๊ธฐ ์ํ DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ ๋๋ค. HTMLCollection๊ณผ NodeList๋ ๋ชจ๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฉด์ ์ดํฐ๋ฌ๋ธ์ด๋ฏ๋ก for ... of ๋ฌธ์ผ๋ก ์ํํ ์ ์์ผ๋ฉฐ ์คํ๋ ๋ ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ๋ฐฐ์ด๋ก ๋ณํํ ์ ์์ต๋๋ค.
- HTMLCollection : ๋ ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณํ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ์ด์ ์๋(live) ๊ฐ์ฒด
- NodeList : ๋๋ถ๋ถ์ ๊ฒฝ์ฐ ๋ ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณํ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ์ง ์๊ณ ๊ณผ๊ฑฐ์ ์ ์ ์ํ๋ฅผ ์ ์งํ๋ non-live ๊ฐ์ฒด๋ก ๋์ (๊ฒฝ์ฐ์ ๋ฐ๋ผ live ๊ฐ์ฒด๋ก ๋์)
HTMLCollection
getElementsByTagName๊ณผ getElementsByClassName ๋ฉ์๋๊ฐ ๋ฐํํ๋ DOM ์ปฌ๋ ์ ๊ฐ์ฒด๋ก live ๊ฐ์ฒด์ ๋๋ค.
๋ฐ๋ผ์ HTMLCollection ๊ฐ์ฒด๋ ์ค์๊ฐ์ผ๋ก ๋ ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณ๊ฒฝ์ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ for ๋ฌธ์ผ๋ก ์ํํ๋ฉด์ ๋ ธ๋ ๊ฐ์ฒด์ ์ํ๋ฅผ ๋ณ๊ฒฝํด์ผ ํ ๋ ์ฃผ์ํด์ผ ํฉ๋๋ค.
HTMLCollection ๊ฐ์ฒด๋ for๋ฌธ ๋์ ๋ค์๊ณผ ๊ฐ์ ์ํ ๋ฐฉ๋ฒ์ด ๊ถ์ฅ๋ฉ๋๋ค.
- for๋ฌธ์ ์ญ๋ฐฉํฅ์ผ๋ก ์ํ
- while ๋ฌธ์ผ๋ก HTMLCollection์ ์์๊ฐ ๋จ์ ์์ง ์์ ๋๊น์ง ๋ฌดํ ๋ฐ๋ณต
- ๊ฐ์ฒด์ ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ๋ฐฐ์ด์ ๊ณ ์ฐจ ํจ์(forEach, map, filter, reduce ๋ฑ)๋ฅผ ์ฌ์ฉ
NodeList
querySelectorAll ๋ฉ์๋๊ฐ ๋ฐํํ๋ DOM ์ปฌ๋ ์ ๊ฐ์ฒด๋ก non-live ๊ฐ์ฒด์ ๋๋ค. ๋จ, childNodes ํ๋กํผํฐ๊ฐ ๋ฐํํ๋ NodeList ๊ฐ์ฒด๋ HTMLCollection ๊ฐ์ฒด์ ๊ฐ์ด live ๊ฐ์ฒด๋ก ๋์ํ๋ฏ๋ก ์ฃผ์๊ฐ ํ์ํฉ๋๋ค.
๋ฐ๋ผ์ ๋ ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณ๊ฒฝ๊ณผ ์๊ด์์ด ์์ ํ๊ฒ DOM ์ปฌ๋ ์ ์ ์ฌ์ฉํ๋ ค๋ฉด HTMLCollection์ด๋ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํ์ฌ ์ฌ์ฉํ๋ ๊ฒ์ด ๊ถ์ฅ๋ฉ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</li>
<li>Banana</li>
</ul>
</body>
<script>
const $fruits = document.getElementById('fruits');
// childNodes ํ๋กํผํฐ๋ NodeList ๊ฐ์ฒด(live)๋ฅผ ๋ฐํํ๋ค.
const { childNodes } = $fruits;
// ์คํ๋ ๋ ๋ฌธ๋ฒ์ ์ฌ์ฉํ์ฌ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํ๋ค.
[...childNodes].forEach(childNode => {
$fruits.removeChild(childNode);
});
// $fruits ์์์ ๋ชจ๋ ์์ ๋
ธ๋๊ฐ ๋ชจ๋ ์ญ์ ๋์๋ค.
console.log(childNodes); // NodeList []
</script>
</html>
๋ ธ๋ ํ์
์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ ๋ค์, ์ทจ๋ํ ์์ ๋ ธ๋๋ฅผ ๊ธฐ์ ์ผ๋ก DOM ํธ๋ฆฌ์ ๋ ธ๋๋ฅผ ์ฎ๊ฒจ ๋ค๋๋ฉฐ ๋ถ๋ชจ, ํ์ , ์์ ๋ ธ๋ ๋ฑ์ ํ์ํด์ผ ํ ๋๊ฐ ์์ต๋๋ค. ์ด๋ฌํ ์ํฉ์ ์ํด DOM ํธ๋ฆฌ ์์ ๋ ธ๋๋ฅผ ํ์ํ ์ ์๋๋ก Node, Element ์ธํฐํ์ด์ค๋ ํธ๋ฆฌ ํ์ ํ๋กํผํฐ๋ฅผ ์ ๊ณตํฉ๋๋ค.
- Node.prototype : parentNode, previousSibling, firstChild, childNodes ํ๋กํผํฐ ์ ๊ณต
- Element.prototype : previousElemnetSibling, nextElemnetSibling, children ํ๋กํผํฐ ์ ๊ณต

์ด๋ฌํ ๋ ธ๋ ํ์ ํ๋กํผํฐ๋ ๋ชจ๋ setter์์ด getter๋ง ์กด์ฌํ์ฌ ์ฐธ์กฐ๋ง ๊ฐ๋ฅํ ์ฝ๊ธฐ ์ ์ฉ ์ ๊ทผ์ ํ๋กํผํฐ์ ๋๋ค.
๊ณต๋ฐฑ ํ ์คํธ ๋ ธ๋
HTML ์์ ์ฌ์ด์ ์คํ์ด์ค, ํญ, ์ค๋ฐ๊ฟ(๊ฐํ) ๋ฑ์ ๊ณต๋ฐฑ ๋ฌธ์๋ ํ ์คํธ ๋ ธ๋์ธ "๊ณต๋ฐฑ ํ ์คํธ ๋ ธ๋"๋ฅผ ์์ฑํฉ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
</html>

๋ฐ๋ผ์ ๋ ธ๋๋ฅผ ํ์ํ ๋๋ ๊ณต๋ฐฑ ๋ฌธ์๊ฐ ์์ฑํ ๊ณต๋ฐฑ ํ ์คํธ ๋ ธ๋์ ์ฃผ์ํด์ผ ํฉ๋๋ค.
์์ ๋ ธ๋ ํ์
์์ ๋ ธ๋๋ฅผ ํ์ํ๊ธฐ ์ํด์๋ ๋ค์๊ณผ ๊ฐ์ ๋ ธ๋ ํ์ ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
ํ๋กํผํฐ | ์ค๋ช |
Node.prototype.childNodes | ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ํ์ํ์ฌ DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ธ NodeList์ ๋ด์ ๋ฐํ |
Element.prototype.children | ์์ ๋ ธ๋ ์ค์์ ์์ ๋ ธ๋๋ง ๋ชจ๋ ํ์ํ์ฌ DOM ์ปฌ๋ ์ ๊ฐ์ฒด์ธ HTMLCollection์ ๋ด์ ๋ฐํ |
Node.prototype.fistChild | ์ฒซ ๋ฒ์งธ ์์ ๋ ธ๋๋ฅผ ๋ฐํ |
Node.prototype.lastChild | ๋ง์ง๋ง ์์ ๋ ธ๋๋ฅผ ๋ฐํ |
Element.prototype.firstElemntChild | ์ฒซ ๋ฒ์งธ ์์ ๋ ธ๋๋ฅผ ๋ฐํ |
Element.prototype.lastElementChild | ๋ง์ง๋ง ์์ ๋ ธ๋๋ฅผ ๋ฐํ |
- Node.prototpye ํ๋กํผํฐ : ์์ ๋ ธ๋์ ํ ์คํธ ๋ ธ๋๋ฅผ ๋ฐํ
- Element.prototype ํ๋กํผํฐ : ์์ ๋ ธ๋๋ง ๋ฐํ (ํ ์คํธ ๋ ธ๋ ํฌํจ X)
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ #fruits ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $fruits = document.getElementById('fruits');
// #fruits ์์์ ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// childNodes ํ๋กํผํฐ๊ฐ ๋ฐํํ NodeList์๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ ํฌํจ๋์ด ์๋ค.
console.log($fruits.childNodes);
// NodeList(7) [text, li.apple, text, li.banana, text, li.orange, text]
// #fruits ์์์ ๋ชจ๋ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// children ํ๋กํผํฐ๊ฐ ๋ฐํํ HTMLCollection์๋ ์์ ๋
ธ๋๋ง ํฌํจ๋์ด ์๋ค.
console.log($fruits.children);
// HTMLCollection(3) [li.apple, li.banana, li.orange]
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstChild ํ๋กํผํฐ๋ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
console.log($fruits.firstChild); // #text
// #fruits ์์์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// lastChild ํ๋กํผํฐ๋ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
console.log($fruits.lastChild); // #text
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstElementChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
console.log($fruits.firstElementChild); // li.apple
// #fruits ์์์ ๋ง์ง๋ง ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// lastElementChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
console.log($fruits.lastElementChild); // li.orange
</script>
</html>
์์ ๋ ธ๋ ์กด์ฌ ํ์ธ
Node.prototype.hasChildNodes ๋ฉ์๋
- ์์ ๋ ธ๋๊ฐ ์กด์ฌํ๋ฉด true๋ฅผ ์์ ๋ ธ๋๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด false๋ฅผ ๋ฐํ
- ์์ ๋ ธ๋์ ์กด์ฌ๋ฅผ ํ ์คํธ ๋ ธ๋์ ์์ ๋ ธ๋์์ ํ์ธ
Element.prototype.childElementCount / Node.prototype.children.length ํ๋กํผํฐ
- ์์ ๋ ธ๋๊ฐ ์กด์ฌํ๋ฉด true๋ฅผ ์์ ๋ ธ๋๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด false๋ฅผ ๋ฐํ
- ์์ ๋ ธ๋์ ์กด์ฌ๋ฅผ ์์ ๋ ธ๋์์ ํ์ธ
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ #fruits ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $fruits = document.getElementById('fruits');
// #fruits ์์์ ์์ ๋
ธ๋๊ฐ ์กด์ฌํ๋์ง ํ์ธํ๋ค.
// hasChildNodes ๋ฉ์๋๋ ํ
์คํธ ๋
ธ๋๋ฅผ ํฌํจํ์ฌ ์์ ๋
ธ๋์ ์กด์ฌ๋ฅผ ํ์ธํ๋ค.
console.log($fruits.hasChildNodes()); // true
// hasChildNodes ๋ฉ์๋๋ ํ
์คํธ ๋
ธ๋๋ฅผ ํฌํจํ์ฌ ์์ ๋
ธ๋์ ์กด์ฌ๋ฅผ ํ์ธํ๋ค.
console.log($fruits.hasChildNodes()); // true
// ์์ ๋
ธ๋ ์ค์ ํ
์คํธ ๋
ธ๋๊ฐ ์๋ ์์ ๋
ธ๋๊ฐ ์กด์ฌํ๋์ง๋ ํ์ธํ๋ค.
console.log(!!$fruits.children.length); // 0 -> false
// ์์ ๋
ธ๋ ์ค์ ํ
์คํธ ๋
ธ๋๊ฐ ์๋ ์์ ๋
ธ๋๊ฐ ์กด์ฌํ๋์ง๋ ํ์ธํ๋ค.
console.log(!!$fruits.childElementCount); // 0 -> false
</script>
</html>
์์ ๋ ธ๋์ ํ ์คํธ ๋ ธ๋ ํ์
์์ ๋ ธ๋์ ํ ์คํธ ๋ ธ๋๋ ์์ ๋ ธ๋์ ์์์ ๋๋ค. ๋ฐ๋ผ์ firstChild ํ๋กํผํฐ๋ก ์์ ๋ ธ๋์ ํ ์คํธ ๋ ธ๋์ ์ ๊ทผํ ์ ์์ต๋๋ค. ์ด๋, firstChild ํ๋กํผํฐ๋ ์ฒซ ๋ฒ์งธ ์์ ๋ ธ๋๋ฅผ ๋ฐํํ๋ฉฐ ํ ์คํธ ๋ ธ๋์ด๊ฑฐ๋ ์์ ๋ ธ๋์ ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
<script>
// ์์ ๋
ธ๋์ ํ
์คํธ ๋
ธ๋๋ firstChild ํ๋กํผํฐ๋ก ์ ๊ทผํ ์ ์๋ค.
console.log(document.getElementById('foo').firstChild); // #text
</script>
</body>
</html>
๋ถ๋ชจ ๋ ธ๋ ํ์
๋ถ๋ชจ ๋ ธ๋๋ฅผ ํ์ํ๋ ค๋ฉด Node.prototype.parentNode ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ํ ์คํธ ๋ ธ๋๋ DOM ํธ๋ฆฌ์ ์ต์ข ๋จ ๋ ธ๋์ธ ๋ฆฌํ ๋ ธ๋์ด๋ฏ๋ก ๋ถ๋ชจ ๋ ธ๋๊ฐ ํ ์คํธ ๋ ธ๋์ธ ๊ฒฝ์ฐ๋ ์์ต๋๋ค.
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ .banana ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $banana = document.querySelector('.banana');
// .banana ์์ ๋
ธ๋์ ๋ถ๋ชจ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
console.log($banana.parentNode); // ul#fruits
</script>
</html>
ํ์ ๋ ธ๋ ํ์
ํ์ ๋ ธ๋๋ฅผ ํ์ํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๋จ, ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๋ ์์ ๋ ธ๋์ ์ฐ๊ฒฐ๋์ด ์์ง๋ง ๋ถ๋ชจ ๋ ธ๋๊ฐ ๊ฐ์ ํ์ ๋ ธ๋๊ฐ ์๋๋ฏ๋ก ๋ฐํ๋์ง ์์ต๋๋ค. ๋ฐ๋ผ์ ์๋ ํ๋กํผํฐ๋ ํ ์คํธ ๋ ธ๋ ๋๋ ์์ ๋ ธ๋๋ง ๋ฐํํฉ๋๋ค.
ํ๋กํผํฐ | ์ค๋ช |
Node.prototype.previousSibling | ๋ถ๋ชจ ๋ ธ๋๊ฐ ๊ฐ์ ํ์ ๋ ธ๋ ์ค์์ ์์ ์ ์ด์ ํ์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ |
Node.prototype.nextSibling | ๋ถ๋ชจ ๋ ธ๋๊ฐ ๊ฐ์ ํ์ ๋ ธ๋ ์ค์์ ์์ ์ ๋ค์ ํ์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ |
Element.prototype.previousElementSibling | ๋ถ๋ชจ ๋ ธ๋๊ฐ ๊ฐ์ ํ์ ๋ ธ๋ ์ค์์ ์์ ์ ์ด์ ํ์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ |
Element.prototype.nextElementSibling | ๋ถ๋ชจ ๋ ธ๋๊ฐ ๊ฐ์ ํ์ ๋ ธ๋ ์ค์์ ์์ ์ ๋ค์ ํ์ ๋ ธ๋๋ฅผ ํ์ํ์ฌ ๋ฐํ |
- Node.prototpye ํ๋กํผํฐ : ์์ ๋ ธ๋์ ํ ์คํธ ๋ ธ๋๋ฅผ ๋ฐํ
- Element.prototype ํ๋กํผํฐ : ์์ ๋ ธ๋๋ง ๋ฐํ (ํ ์คํธ ๋ ธ๋ ํฌํจ X)
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li class="apple">Apple</li>
<li class="banana">Banana</li>
<li class="orange">Orange</li>
</ul>
</body>
<script>
// ๋
ธ๋ ํ์์ ๊ธฐ์ ์ด ๋๋ #fruits ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $fruits = document.getElementById('fruits');
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
const { firstChild } = $fruits;
console.log(firstChild); // #text
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ๋
ธ๋(ํ
์คํธ ๋
ธ๋)์ ๋ค์ ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// nextSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
const { nextSibling } = firstChild;
console.log(nextSibling); // li.apple
// li.apple ์์์ ์ด์ ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// previousSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ฟ๋ง ์๋๋ผ ํ
์คํธ ๋
ธ๋๋ฅผ ๋ฐํํ ์๋ ์๋ค.
const { previousSibling } = nextSibling;
console.log(previousSibling); // #text
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// firstElementChild ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
const { firstElementChild } = $fruits;
console.log(firstElementChild); // li.apple
// #fruits ์์์ ์ฒซ ๋ฒ์งธ ์์ ์์ ๋
ธ๋(li.apple)์ ๋ค์ ํ์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// nextElementSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
const { nextElementSibling } = firstElementChild;
console.log(nextElementSibling); // li.banana
// li.banana ์์์ ์ด์ ํ์ ์์ ๋
ธ๋๋ฅผ ํ์ํ๋ค.
// previousElementSibling ํ๋กํผํฐ๋ ์์ ๋
ธ๋๋ง ๋ฐํํ๋ค.
const { previousElementSibling } = nextElementSibling;
console.log(previousElementSibling); // li.apple
</script>
</html>
๋ ธ๋ ์ ๋ณด ์ทจ๋
๋ ธ๋ ๊ฐ์ฒด์ ๋ํ ์ ๋ณด๋ฅผ ์ทจ๋ํ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ ธ๋ ์ ๋ณด ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํฉ๋๋ค.
ํ๋กํผํฐ | ์ค๋ช |
Node.prototype.nodeType | ๋
ธ๋ ๊ฐ์ฒด์ ์ข
๋ฅ(ํ์
)์ ๋ํ๋ด๋ ์ ์๋ฅผ ๋ฐํ (์์๋ Node์ ์ ์๋์ด ์์) - Node.ELEMENT_NODE : ์์ ๋ ธ๋ ํ์ ์ ๋ํ๋ด๋ ์์ 1 ๋ฐํ - Node.TEXT_NODE : ํ ์คํธ ๋ ธ๋ ํ์ ์ ๋ํ๋ด๋ ์์ 3 ๋ฐํ - Node.DOCUMENT_NODE : ๋ฌธ์ ๋ ธ๋ ํ์ ์ ๋ํ๋ด๋ ์์ 9 ๋ฐํ |
Node.prototype.nodeName | ๋
ธ๋ ์ด๋ฆ์ ๋ฌธ์์ด๋ก ๋ฐํ - ์์ ๋ ธ๋ : ๋๋ฌธ์ ๋ฌธ์์ด๋ก ํ๊ทธ ์ด๋ฆ("UL, "LI" ๋ฑ)์ ๋ฐํ - ํ ์คํธ ๋ ธ๋ : ๋ฌธ์์ด "#text"๋ฅผ ๋ฐํ - ๋ฌธ์ ๋ ธ๋ : ๋ฌธ์์ด "#document"๋ฅผ ๋ฐํ |
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// ๋ฌธ์ ๋
ธ๋์ ๋
ธ๋ ์ ๋ณด๋ฅผ ์ทจ๋ํ๋ค.
console.log(document.nodeType); // 9
console.log(document.nodeName); // #document
// ์์ ๋
ธ๋์ ๋
ธ๋ ์ ๋ณด๋ฅผ ์ทจ๋ํ๋ค.
const $foo = document.getElementById('foo');
console.log($foo.nodeType); // 1
console.log($foo.nodeName); // DIV
// ํ
์คํธ ๋
ธ๋์ ๋
ธ๋ ์ ๋ณด๋ฅผ ์ทจ๋ํ๋ค.
const $textNode = $foo.firstChild;
console.log($textNode.nodeType); // 3
console.log($textNode.nodeName); // #text
</script>
</html>
์์ ๋ ธ๋์ ํ ์คํธ ์กฐ์
nodeValue
Node.prototype.nodeValue ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก์ nodeValue ํ๋กํผํฐ๋ ์ฐธ์กฐ์ ํ ๋น ๋ชจ๋ ๊ฐ๋ฅํฉ๋๋ค.
๋ ธ๋ ๊ฐ์ฒด์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด ๋ ธ๋ ๊ฐ์ฒด์ ๊ฐ์ธ ํ ์คํธ ๋ ธ๋์ ํ ์คํธ๋ฅผ ๋ฐํํฉ๋๋ค. (๋ฌธ์ ๋ ธ๋๋ ์์ ๋ ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด null์ ๋ฐํ)
nodeValue ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ ์์ ๋ ธ๋์ ํ ์คํธ ์กฐ์ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ํ ์คํธ๋ฅผ ๋ณ๊ฒฝํ ์์ ๋ ธ๋๋ฅผ ์ทจ๋ํ ๋ค์, ์ทจ๋ํ ์์ ๋ ธ๋์ ํ ์คํธ ๋ ธ๋๋ฅผ firstChild ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํด ํ์
- ํ์ํ ํ ์คํธ ๋ ธ๋์ nodeValue ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ ์คํธ ๋ ธ๋์ ๊ฐ์ ๋ณ๊ฒฝ
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello</div>
</body>
<script>
// 1. #foo ์์ ๋
ธ๋์ ์์ ๋
ธ๋์ธ ํ
์คํธ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
const $textNode = document.getElementById('foo').firstChild;
// 2. nodeValue ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ ํ
์คํธ ๋
ธ๋์ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
$textNode.nodeValue = 'World';
console.log($textNode.nodeValue); // World
</script>
</html>
textContent
Node.prototype.textContent ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก์ ์์ ๋ ธ๋์ ํ ์คํธ์ ๋ชจ๋ ์์ ๋ ธ๋์ ํ ์คํธ๋ฅผ ๋ชจ๋ ์ทจ๋ํ๊ฑฐ๋ ๋ณ๊ฒฝํฉ๋๋ค.
์์ ๋ ธ๋์ textContent ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด ์์ ๋ ธ๋์ ์ฝํ ์ธ ์์ญ(์์ ํ๊ทธ์ ์ข ๋ฃ ํ๊ทธ ์ฌ์ด) ๋ด์ ํ ์คํธ๋ฅผ ๋ชจ๋ ๋ฐํํฉ๋๋ค. ์ฆ, ์์ ๋ ธ๋์ childNodes ํ๋กํผํฐ๊ฐ ๋ฐํํ ๋ชจ๋ ๋ ธ๋๋ค์ ํ ์คํธ๋ฅผ ๋ชจ๋ ๋ฐํํฉ๋๋ค. ์ด๋ HTML ๋งํฌ์ ์ ๋ฌด์๋ฉ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// #foo ์์ ๋
ธ๋์ ํ
์คํธ๋ฅผ ๋ชจ๋ ์ทจ๋ํ๋ค. ์ด๋ HTML ๋งํฌ์
์ ๋ฌด์๋๋ค.
console.log(document.getElementById('foo').textContent); // Hello world!
</script>
</html>
์์ ๋ ธ๋์ textContent ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด ์์ ๋ ธ๋์ ๋ชจ๋ ์์ ๋ ธ๋๊ฐ ์ ๊ฑฐ๋๊ณ ํ ๋นํ ๋ฌธ์์ด์ด ํ ์คํธ๋ก ์ถ๊ฐ๋ฉ๋๋ค. ์ด๋ ํ ๋นํ ๋ฌธ์์ด์ HTML ๋งํฌ์ ์ด ํฌํจ๋์ด ์๋๋ผ๋ ๋ฌธ์์ด ๊ทธ๋๋ก ์ธ์๋์ด ํ ์คํธ๋ก ์ทจ๊ธ๋ฉ๋๋ค.
์์ ์ดํด๋ณธ nodeValue ํ๋กํผํฐ์ ๋นํด textContent ํ๋กํผํฐ๋ ๊ฐ๊ฒฐํ ์ฝ๋๋ก ํ ์คํธ๋ฅผ ์ทจ๋ํ ์ ์์ต๋๋ค.
DOM ์กฐ์
DOM ์กฐ์์ ์๋ก์ด ๋ ธ๋๋ฅผ ์์ฑํ์ฌ DOM์ ์ถ๊ฐํ๊ฑฐ๋ ๊ธฐ์กด ๋ ธ๋๋ฅผ ์ญ์ ๋๋ ๊ต์ฒดํ๋ ๊ฒ์ ๋งํฉ๋๋ค. ์ด๋, ๋ฆฌํ๋ก์ฐ์ ๋ฆฌํ์ธํธ๊ฐ ๋ฐ์ํ๊ธฐ ๋๋ฌธ์ ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํด ์ฃผ์ํด์ ๋ค๋ฃจ์ด์ผ ํฉ๋๋ค.
innerHTML
Element.prototype.innerHTML ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก์ ์์ ๋ ธ๋์ HTML ๋งํฌ์ ์ ์ทจ๋ํ๊ฑฐ๋ ๋ณ๊ฒฝํฉ๋๋ค. ์์ ๋ ธ๋์ innerHTML ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด ์์ ๋ ธ๋์ ์ฝํ ์ธ ์์ญ ๋ด์ ํฌํจ๋ ๋ชจ๋ HTML ๋งํฌ์ ์ ๋ฌธ์์ด๋ก ๋ฐํํฉ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// #foo ์์์ ์ฝํ
์ธ ์์ญ ๋ด์ HTML ๋งํฌ์
์ ๋ฌธ์์ด๋ก ์ทจ๋ํ๋ค.
console.log(document.getElementById('foo').innerHTML);
// "Hello <span>world!</span>"
</script>
</html>
์์ ๋ ธ๋์ innerHTML ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด ์์ ๋ ธ๋์ ๋ชจ๋ ์์ ๋ ธ๋๊ฐ ์ ๊ฑฐ๋๊ณ ํ ๋นํ ๋ฌธ์์ด์ ํฌํจ๋์ด ์๋ HTML ๋งํฌ์ ์ด ํ์ฑ๋์ด ์์ ๋ ธ๋์ ์์ ๋ ธ๋๋ก DOM์ ๋ฐ์๋ฉ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<div id="foo">Hello <span>world!</span></div>
</body>
<script>
// HTML ๋งํฌ์
์ด ํ์ฑ๋์ด ์์ ๋
ธ๋์ ์์ ๋
ธ๋๋ก DOM์ ๋ฐ์๋๋ค.
document.getElementById('foo').innerHTML = 'Hi <span>there!</span>';
</script>
</html>
์ด์ฒ๋ผ innerHTML ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ฉด HTML ๋งํฌ์ ๋ฌธ์์ด๋ก ๊ฐ๋จํ DOM ์กฐ์์ด ๊ฐ๋ฅํ๋ค๋ ์ฅ์ ์ด ์์ง๋ง, ์๋์ ๊ฐ์ ๋จ์ ์ด ์กด์ฌํฉ๋๋ค.
- ์ฌ์ฉ์๋ก๋ถํฐ ์ ๋ ฅ๋ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก innerHTML ํ๋กํผํฐ์ ํ ๋นํ๋ ๊ฒ์ XSS(ํฌ๋ก์ค ์ฌ์ดํธ ์คํฌ๋ฆฝํธ ๊ณต๊ฒฉ)์ ์ทจ์ฝํ๋ฏ๋ก ์ํํจ
- innerHTML ํ๋กํผํฐ์ HTML ๋งํฌ์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด ๊ธฐ์กด์ ์์ ๋ ธ๋๋ฅผ ๋ชจ๋ ์ ๊ฑฐํ๊ณ ์๋กญ๊ฒ ์์ ๋ ธ๋๋ฅผ ์์ฑํ๋ฉฐ DOM์ ๋ฐ์๋๊ธฐ ๋๋ฌธ์ ํจ์จ์ ์ด์ง ๋ชปํจ
- innerHTML ํ๋กํผํฐ๋ ์๋ก์ด ์์๋ฅผ ์ฝ์ ํ ๋ ์ฝ์ ๋ ์์น๋ฅผ ์ง์ ํ ์ ์์
insertAdjacentHTML ๋ฉ์๋
Element.prototype.insertAdjacentHTML(position, DOMString) ๋ฉ์๋๋ ๊ธฐ์กด ์์๋ฅผ ์ ๊ฑฐํ์ง ์์ผ๋ฉด์ ์์น๋ฅผ ์ง์ ํด
์๋ก์ด ์์๋ฅผ ์ฝ์ ํฉ๋๋ค. ์ด๋, insertAdjacentHTML ๋ฉ์๋๋ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ HTML ๋งํฌ์ ๋ฌธ์์ด(DOMStirng)์ ํ์ฑํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ก ์์ฑ๋ ๋ ธ๋๋ฅผ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ์์น(position)์ ์ฝ์ ํ์ฌ DOM์ ๋ฐ์ํฉ๋๋ค.
์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ์ ์๋ ๋ฌธ์์ด์ 'beforebegin', 'afterbegin', beforeend', 'afterend'์ 4๊ฐ์ง ์ ๋๋ค.
<!DOCTYPE html>
<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>
์ด์ฒ๋ผ ๊ธฐ์กด ์์์๋ ์ํฅ์ ์ฃผ์ง ์๊ณ ์๋กญ๊ฒ ์ฝ์ ๋ ์์๋ง์ ํ์ฑํ์ฌ ์์ ์์๋ก ์ถ๊ฐํ๋ฏ๋ก innerHTML ํ๋กํผํฐ๋ณด๋ค ํจ์จ์ ์ด๊ณ ๋น ๋ฅด๋ค๋ ์ฅ์ ์ด ์์ง๋ง, ํฌ๋ก์ค ์ฌ์ดํธ ์คํฌ๋ฆฝํ ๊ณต๊ฒฉ์ ์ทจ์ฝํ๋ ์ ์ ๋์ผํฉ๋๋ค.
๋ ธ๋ ์์ฑ๊ณผ ์ถ๊ฐ
DOM์ ๋ ธ๋๋ฅผ ์ง์ ์์ฑ/์ฝ์ /์ญ์ /์นํํ๋ ๋ฉ์๋๋ ์ ๊ณตํฉ๋๋ค.
๋ฉ์๋ | ์ค๋ช |
Document.prototype.createElement(tagName) | ํ๊ทธ ์ด๋ฆ์ ๋ํ๋ด๋ ๋ฌธ์์ด์ ์ธ์๋ก ์ ๋ฌํด ์์ ๋ ธ๋ ์์ฑํ์ฌ ๋ฐํ |
Document.prototype.createTextNode(text) | ํ ์คํธ ๋ ธ๋์ ๊ฐ์ผ๋ก ์ฌ์ฉํ ๋ฌธ์์ด์ ์ธ์๋ก ์ ๋ฌํด ํ ์คํธ ๋ ธ๋ ์์ฑ |
Node.prototype.appendChild(childNode) | ๋งค๊ฐ๋ณ์ childNode์๊ฒ ์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ฅผ appendChild ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ธ๋์ ๋ง์ง๋ง ์์ ๋ ธ๋๋ก ์ถ๊ฐ |
<!DOCTYPE html>
<html>
<body>
<ul id="fruits">
<li>Apple</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>
๋ณต์์ ๋ ธ๋ ์์ฑ๊ณผ ์ถ๊ฐ
์ฌ๋ฌ ๊ฐ์ ์์ ๋ ธ๋๋ฅผ ์์ฑํ์ฌ DOM์ ์ถ๊ฐํ ์ ์์ต๋๋ค. ์ด๋, DOM์ด ๋ณ๊ฒฝ๋ ๋ ์คํ๋๋ ๋ฆฌํ๋ก์ฐ์ ๋ฆฌํ์ธํธ์ ํ์๋ฅผ ์ค์ฌ ์ฑ๋ฅ์ ๋์ด๊ธฐ ์ํด DocumentFragment ๋ ธ๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
DocumentFragment ๋ ธ๋๋ ๋ฌธ์, ์์, ์ดํธ๋ฆฌ๋ทฐํธ, ํ ์คํธ ๋ ธ๋์ ๊ฐ์ ๋ ธ๋ ๊ฐ์ฒด์ ์ผ์ข ์ผ๋ก, ๋ถ๋ชจ ๋ ธ๋๊ฐ ์์ด์ ๊ธฐ์กด DOM๊ณผ๋ ๋ณ๋๋ก ์กด์ฌํ๋ค๋ ํน์ง์ด ์์ต๋๋ค. ์ด๋ฅผ ํตํด ๋ณ๋์ ์๋ธ DOM์ ๊ตฌ์ฑํ์ฌ ๊ธฐ์กด DOM์ ์ถ๊ฐํฉ๋๋ค.
Document.prototype.createDocumnetFragment ๋ฉ์๋๋ ๋น์ด ์๋ Documentfragment ๋ ธ๋๋ฅผ ์์ฑํ์ฌ ๋ฐํํฉ๋๋ค.
<!DOCTYPE html>
<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>
๋ ธ๋ ์ฝ์
๋ฉ์๋ | ์ค๋ช |
Node.prototype.appendChild | - ๋ง์ง๋ง ๋
ธ๋๋ก ์ถ๊ฐ - ๋ ธ๋๋ฅผ ์ถ๊ฐํ ์์น๋ฅผ ์ง์ ํ ์ ์์ |
Node.prototype.insertBefore(newNode, childNode) | - ์ง์ ํ ์์น์ ๋
ธ๋ ์ฝ์
- newNode๋ฅผ childNode ์์ ์ฝ์ - childNode๋ ๋ฐ๋์ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ธ๋์ ์์ ๋ ธ๋ |
๋ ธ๋ ์ด๋
DOM์ ์ด๋ฏธ ์กด์ฌํ๋ ๋ ธ๋๋ฅผ appendChild ๋๋ insertBefore ๋ฉ์๋๋ฅผ ์ฌ์ฉํด DOM์ ๋ค์ ์ถ๊ฐํ๋ฉด ํ์ฌ ์์น์์ ๋ ธ๋๋ฅผ ์ ๊ฑฐํ๊ณ ์๋ก์ด ์์น์ ๋ ธ๋๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ์์ผ๋ก ๋ ธ๋๋ฅผ ์ด๋ํ ์ ์์ต๋๋ค.
<!DOCTYPE html>
<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>
๋ ธ๋ ๋ณต์ฌ
Node.prototype.cloneNode([deep: true | false]) ๋ฉ์๋๋ ๋ ธ๋์ ์ฌ๋ณธ์ ์์ฑํ์ฌ ๋ฐํํฉ๋๋ค. ๋งค๊ฐ๋ณ์ deep์ true๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ฉด ๋ ธ๋๋ฅผ ๊น์ ๋ณต์ฌํ์ฌ ๋ชจ๋ ์์ ๋ ธ๋๊ฐ ํฌํจ๋ ์ฌ๋ณธ์ ์์ฑํ๊ณ , false๋ฅผ ์ธ์๋ก ์ ๋ฌํ๋ฉด ์๋ตํ๋ฉด์ ๋ ธ๋๋ฅผ ์์ ๋ณต์ฌํ์ฌ ๋ ธ๋ ์์ ๋ง์ ์ฌ๋ณธ์ ์์ฑํฉ๋๋ค.
<!DOCTYPE html>
<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>
๋ ธ๋ ๊ต์ฒด
Node.prototype.replaceChild(newChild, oldChild) ๋ฉ์๋๋ ์์ ์ ํธ์ถํ ๋ ธ๋์ ์์ ๋ ธ๋์ธ oldChild๋ฅผ ๋ค๋ฅธ ๋ ธ๋์ธ newChild๋ก ๊ต์ฒดํฉ๋๋ค. ์ด๋, oldChild ๋ ธ๋๋ DOM์์ ์ ๊ฑฐ๋ฉ๋๋ค.
<!DOCTYPE html>
<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>
๋ ธ๋ ์ญ์
Node.prototype.removeChild(child) ๋ฉ์๋๋ child ๋งค๊ฐ๋ณ์์ ์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ฅผ DOM์์ ์ญ์ ํ๋ฉฐ ์ธ์๋ก ์ ๋ฌํ ๋ ธ๋๋ removeChild ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ธ๋์ ์์ ๋ ธ๋์ด์ด์ผ ํฉ๋๋ค.
<!DOCTYPE html>
<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>
์ดํธ๋ฆฌ๋ทฐํธ
์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋์ attributes ํ๋กํผํฐ
HTML ๋ฌธ์์ ๊ตฌ์ฑ ์์์ธ HTML ์์๋ ์ฌ๋ฌ ๊ฐ์ ์ดํธ๋ฆฌ๋ทฐํธ(์์ฑ)๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. HTML ์์ ๋์์ ์ ์ดํ๊ธฐ ์ํ ์ถ๊ฐ์ ์ธ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ ์ดํธ๋ฆฌ๋ทฐํธ๋ HTML ์์์ ์์ ํ๊ทธ์ ์ดํธ๋ฆฌ๋ทฐํธ ์ด๋ฆ = "์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ" ํ์์ผ๋ก ์ ์ํฉ๋๋ค.
<input id="user" type="text" value="ungmo2">
HTML ๋ฌธ์๊ฐ ํ์ฑ๋ ๋ HTML ์์์ ์ดํธ๋ฆฌ๋ทฐํธ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๋ก ๋ณํ๋์ด ์์ ๋ ธ๋์ ์ฐ๊ฒฐ๋ฉ๋๋ค. ์ด๋ HTML ์ดํธ๋ฆฌ๋ทฐํธ๋น ํ๋์ ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋๊ฐ ์์ฑ๋๋ฉฐ ๋ชจ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋ ธ๋์ ์ฐธ์กฐ๋ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด์ ์ดํฐ๋ฌ๋ธ์ธ NameNodeMap ๊ฐ์ฒด์ ๋ด๊ฒจ ์์ ๋ ธ๋์ attributes ํ๋กํผํฐ์ ์ ์ฅ๋ฉ๋๋ค.
์ด๋ฌํ NameNodeMap ๊ฐ์ฒด๋ ์์ ๋ ธ๋์ Element.prototype.attributes ํ๋กํผํฐ์ ์ํด ๋ฐํ๋๋ฉฐ attributes ํ๋กํผํฐ๋ getter๋ง ์กด์ฌํ๋ ์ฝ๊ธฐ ์ ์ฉ ์ ๊ทผ์ ํ๋กํผํฐ์ ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<input id="user" type="text" value="ungmo2">
<script>
// ์์ ๋
ธ๋์ attribute ํ๋กํผํฐ๋ ์์ ๋
ธ๋์ ๋ชจ๋ ์ดํธ๋ฆฌ๋ทฐํธ ๋
ธ๋์ ์ฐธ์กฐ๊ฐ ๋ด๊ธด NamedNodeMap ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
const { attributes } = document.getElementById('user');
console.log(attributes);
// NamedNodeMap {0: id, 1: type, 2: value, id: id, type: type, value: value, length: 3}
// ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ์ทจ๋
console.log(attributes.id.value); // user
console.log(attributes.type.value); // text
console.log(attributes.value.value); // ungmo2
</script>
</body>
</html>
HTML ์ดํธ๋ฆฌ๋ทฐํธ ์กฐ์
๋ค์๊ณผ ๊ฐ์ ๋ฉ์๋๋ค์ ํตํด attributes ํ๋กํผํฐ๋ฅผ ํตํ์ง ์๊ณ ์์ ๋ ธ๋์์ ๋ฉ์๋๋ฅผ ํตํด ์ง์ HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์กฐ์ํ ์ ์์ต๋๋ค.
๋ฉ์๋ | ์ค๋ช |
Element.prototype.getAttrubute(attributeName) | attributes ํ๋กํผํฐ๋ฅผ ํตํ์ง ์๊ณ ์์ ๋ ธ๋์์ ๋ฉ์๋๋ฅผ ํตํด ์ง์ HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋ |
Element.prototype.setAttribute(attributeName, atrributeVale) | attributes ํ๋กํผํฐ๋ฅผ ํตํ์ง ์๊ณ ์์ ๋ ธ๋์์ ๋ฉ์๋๋ฅผ ํตํด ์ง์ HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๋ณ๊ฒฝ |
Element.prototype.hasAttrubute(attributeName) | ํน์ HTML ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์กด์ฌํ๋์ง ํ์ธ |
Element.prototype.removeAttrubute(attributeName) | ํน์ HTML ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ญ์ |
<!DOCTYPE html>
<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
// foo ์ดํธ๋ฆฌ๋ทฐํธ์ ์กด์ฌ ํ์ธ
if ($input.hasAttribute('foo')) {
// foo ์ดํธ๋ฆฌ๋ทฐํธ ์ญ์
$input.removeAttribute('foo');
}
// foo ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์ญ์ ๋์๋ค.
console.log($input.hasAttribute('foo')); // false
</script>
</body>
</html>
HTML ์ดํธ๋ฆฌ๋ทฐํธ vs. DOM ํ๋กํผํฐ
์์ ๋ ธ๋ ๊ฐ์ฒด์๋ HTML ์ดํธ๋ฆฌ๋ทฐํธ์ DOM ํ๋กํผํฐ๊ฐ ์กด์ฌํฉ๋๋ค. HTML ์ดํธ๋ฆฌ๋ทฐํธ๋ HTML ์์์ ์ด๊ธฐ ์ํ๋ฅผ ๊ด๋ฆฌํ๊ณ , DOM ํ๋กํผํฐ๋ค์ HTML ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ด๊ธฐ๊ฐ์ผ๋ก ๊ฐ์ง๊ณ ์์ผ๋ฉฐ ์์ ๋ ธ๋์ ์ต์ ์ํ๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.
์ด๋, ์ฌ์ฉ์ ์ ๋ ฅ์ ์ํ ์ํ ๋ณํ์ ๊ด๊ณ์๋ DOM ํ๋กํผํฐ๋ง ์ต์ ์ํ ๊ฐ์ ๊ด๋ฆฌํ๊ณ ๊ทธ ์ธ์ ์ฌ์ฉ์ ์ ๋ ฅ์ ์ํ ์ํ ๋ณํ์ ๊ด๊ณ์๋ ์ดํธ๋ฆฌ๋ทฐํธ์ DOM ํ๋กํผํฐ๋ ํญ์ ๋์ผํ ๊ฐ์ผ๋ก ์ฐ๋๋ฉ๋๋ค.
- id ์ดํธ๋ฆฌ๋ทฐํธ์ id ํ๋กํผํฐ๋ 1:1 ๋์, ๋์ผํ ๊ฐ์ผ๋ก ์ฐ๋
- input ์์์ value ์ดํธ๋ฆฌ๋ทฐํธ๋ value ํ๋กํผํฐ์ 1:1 ๋์, value ์ดํธ๋ฆฌ๋ทฐํธ๋ ์ด๊ธฐ๊ฐ์ ๊ฐ์ง๊ณ value ํ๋กํผํฐ๋ ์ต์ ์ํ๋ฅผ ๊ฐ์ง
- class ์ดํธ๋ฆฌ๋ทฐํธ๋ className, classList ํ๋กํผํฐ์ ๋์ํจ
- for ์ดํธ๋ฆฌ๋ทฐํธ๋ htmlFor ํ๋กํผํฐ์ 1:1 ๋์ํจ
- td ์์์ colspan ์ดํธ๋ฆฌ๋ทฐํธ์ textContent ํ๋กํผํฐ๋ ๋์ํ๋ ํ๋กํผํฐ๊ฐ ์กด์ฌX
- ์ดํธ๋ฆฌ๋ทฐํธ ์ด๋ฆ์ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ณํ์ง ์์ง๋ง ํ๋กํผํฐ ํค๋ ์นด๋ฉ ์ผ์ด์ค๋ฅผ ๋ฐ๋ฆ
data ์ดํธ๋ฆฌ๋ทฐํธ์ dataset ํ๋กํผํฐ
data ์ดํธ๋ฆฌ๋ทฐํธ์ dataset ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ๋ฉด HTML ์์์ ์ ์ํ ์ฌ์ฉ์ ์ ์ ์ดํธ๋ฆฌ๋ทฐํธ์ ์๋ฐ์คํฌ๋ฆฝํธ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ๊ตํํ ์ ์์ต๋๋ค. data ์ดํธ๋ฆฌ๋ทฐํธ๋ data-user-id, data-role๊ณผ ๊ฐ์ด data- ์ ๋์ฌ ๋ค์์ ์์์ ์ด๋ฆ์ ๋ถ์ฌ ์ฌ์ฉํฉ๋๋ค.
data ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ์๋์ ๊ฐ์ด HTMLElement.dataset ํ๋กํผํฐ๋ก ์ทจ๋ํฉ๋๋ค.
- dataset ํ๋กํผํฐ๋ HTML ์์์ ๋ชจ๋ data ์ดํธ๋ฆฌ๋ทฐํธ์ ์ ๋ณด๋ฅผ ์ ๊ณตํ๋ DOMStringMap ๊ฐ์ฒด๋ฅผ ๋ฐํ
- DomStringMap ๊ฐ์ฒด๋ data ์ดํธ๋ฆฌ๋ทฐํธ์ data- ์ ๋์ฌ ๋ค์์ ๋ถ์ธ ์์์ ์ด๋ฆ์ ์นด๋ฉ์ผ์ด์ค๋ก ๋ณํํ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ด ํด๋น ํ๋กํผํฐ๋ฅผ ํตํด data ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ์ทจ๋/๋ณ๊ฒฝ
- ์กด์ฌํ์ง ์๋ ์ด๋ฆ์ ํค๋ก ์ฌ์ฉํ์ฌ dataset ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ๋ฉด HTML ์์์ data ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ ์ถ๊ฐ๋จ
<!DOCTYPE html>
<html>
<body>
<ul class="users">
<li id="1" data-user-id="7621" data-role="admin">Lee</li>
<li id="2" data-user-id="9524">Kim</li>
</ul>
<script>
const users = [...document.querySelector('.users').children];
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
let user = users.find(user => user.dataset.userId === '7621');
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋์์ data-role์ ๊ฐ์ ์ทจ๋ํ๋ค.
console.log(user.dataset.role); // "admin"
// user-id๊ฐ '7621'์ธ ์์ ๋
ธ๋์ data-role ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
user.dataset.role = 'subscriber';
// dataset ํ๋กํผํฐ๋ DOMStringMap ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
console.log(user.dataset); // DOMStringMap {userId: "7621", role: "subscriber"}
// user-id๊ฐ '9524'์ธ ์์ ๋
ธ๋๋ฅผ ์ทจ๋ํ๋ค.
user = users.find(user => user.dataset.userId === '9524');
// user-id๊ฐ '9524'์ธ ์์ ๋
ธ๋์ ์๋ก์ด data ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์ถ๊ฐํ๋ค.
user.dataset.role = 'admin';
console.log(user.dataset);
/*
DOMStringMap {userId: "9524", role: "admin"}
-> <li id="2" data-user-id="9524" data-role="admin">Kim</li>
*/
</script>
</body>
</html>
์คํ์ผ
์ธ๋ผ์ธ ์คํ์ผ ์กฐ์
HTMLElement.prototype.style ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก์ ์์ ๋ ธ๋์ ์ธ๋ผ์ธ ์คํ์ผ์ ์ทจ๋/์ถ๊ฐ/๋ณ๊ฒฝํฉ๋๋ค.
<!DOCTYPE html>
<html>
<body>
<div style="color: red">Hello World</div>
<script>
const $div = document.querySelector('div');
// ์ธ๋ผ์ธ ์คํ์ผ ์ทจ๋
console.log($div.style); // CSSStyleDeclaration { 0: "color", ... }
// ์ธ๋ผ์ธ ์คํ์ผ ๋ณ๊ฒฝ
$div.style.color = 'blue';
// ์ธ๋ผ์ธ ์คํ์ผ ์ถ๊ฐ
$div.style.width = '100px';
$div.style.height = '100px';
$div.style.backgroundColor = 'yellow';
</script>
</body>
</html>
style ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด CSSStyleDeclaration ํ์ ์ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค. ๋ฐํ๋ ๊ฐ์ฒด๋ ๋ค์ํ CSS ํ๋กํผํฐ์ ๋์ํ๋ ํ๋กํผํฐ๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์ด ํ๋กํผํฐ์ ๊ฐ์ ํ ๋นํ๋ฉด ํด๋น CSS ํ๋กํผํฐ๊ฐ ์ธ๋ผ์ธ ์คํ์ผ๋ก HTML ์์์ ์ถ๊ฐ/๋ณ๊ฒฝ๋ฉ๋๋ค.
CSS ํ๋กํผํฐ๋ ์ผ๋ฐฅ ์ผ์ด์ค๋ฅผ ๋ฐ๋ฅด๋ ๋ฐ๋ฉด, CSSStyleDeclaration ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ์นด๋ฉ ์ผ์ด์ค๋ฅผ ๋ฐ๋ฆ ๋๋ค.
// CSS ํ๋กํผํฐ
$div.style['background-color'] = 'yellow';
// CSSStypeDeclaration
$div.style.backgroundColor = 'yellow';
ํด๋์ค ์กฐ์
.์ผ๋ก ์์ํ๋ ํด๋์ค ์ ํ์๋ฅผ ์ฌ์ฉํ์ฌ CSS class๋ฅผ ๋ฏธ๋ฆฌ ์ ์ํ ๋ค์, HTML ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ๊ฐ์ ๋ณ๊ฒฝํ์ฌ HTML ์์์ ์คํ์ผ์ ๋ณ๊ฒฝํ ์๋ ์์ต๋๋ค. ์ด๋, HTML ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ์กฐ์ํ๋ ค๋ฉด class ์ดํธ๋ฆฌ๋ทฐํธ์ ๋์ํ๋ ์์ ๋ ธ๋์ DOM ํ๋กํผํฐ(className๊ณผ classList)๋ฅผ ์ฌ์ฉํฉ๋๋ค.
className
Element.prototype.className ํ๋กํผํฐ๋ setter์ getter ๋ชจ๋ ์กด์ฌํ๋ ์ ๊ทผ์ ํ๋กํผํฐ๋ก์ HTML ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋/๋ณ๊ฒฝํฉ๋๋ค.
- ์์ ๋ ธ๋์ className ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ฉด class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ๋ฌธ์์ด๋ก ๋ฐํ
- ์์ ๋ ธ๋์ className ํ๋กํผํฐ์ ๋ฌธ์์ด์ ํ ๋นํ๋ฉด class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ํ ๋นํ ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px; height: 100px;
background-color: antiquewhite;
}
.red { color: red; }
.blue { color: blue; }
</style>
</head>
<body>
<div class="box red">Hello World</div>
<script>
const $box = document.querySelector('.box');
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ ์ทจ๋
console.log($box.className); // 'box red'
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ์ค์์ 'red'๋ง 'blue'๋ก ๋ณ๊ฒฝ
$box.className = $box.className.replace('red', 'blue');
</script>
</body>
</html>
์ด๋ฌํ className ํ๋กํผํฐ๋ ๋ฌธ์์ด์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋ ์ฌ๋ฌ ๊ฐ์ ํด๋์ค๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ ๋ค๋ฃจ๊ธฐ ๋ถํธํ๋ค๋ ๋จ์ ์ด ์์ต๋๋ค.
classList
Element.prototype.classList ํ๋กํผํฐ๋ class ์ดํธ๋ฆฌ๋ทฐํธ์ ์ ๋ณด๋ฅผ ๋ด์ DOMTokenList ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค.
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px; height: 100px;
background-color: antiquewhite;
}
.red { color: red; }
.blue { color: blue; }
</style>
</head>
<body>
<div class="box red">Hello World</div>
<script>
const $box = document.querySelector('.box');
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ์ ๋ณด๋ฅผ ๋ด์ DOMTokenList ๊ฐ์ฒด๋ฅผ ์ทจ๋
// classList๊ฐ ๋ฐํํ๋ DOMTokenList ๊ฐ์ฒด๋ HTMLCollection๊ณผ NodeList์ ๊ฐ์ด
// ๋
ธ๋ ๊ฐ์ฒด์ ์ํ ๋ณํ๋ฅผ ์ค์๊ฐ์ผ๋ก ๋ฐ์ํ๋ ์ด์ ์๋(live) ๊ฐ์ฒด๋ค.
console.log($box.classList);
// DOMTokenList(2) [length: 2, value: "box blue", 0: "box", 1: "blue"]
// .box ์์์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ ์ค์์ 'red'๋ง 'blue'๋ก ๋ณ๊ฒฝ
$box.classList.replace('red', 'blue');
</script>
</body>
</html>
DOMTokenList ๊ฐ์ฒด๋ class ์ดํธ๋ฆฌ๋ทฐํธ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ์ปฌ๋ ์ ๊ฐ์ฒด๋ก์ ์ ์ฌ ๋ฐฐ์ด ๊ฐ์ฒด์ด๋ฉด์ ์ดํฐ๋ฌ๋ธ์ ๋๋ค. ๋ํ, DOMTokenList ๊ฐ์ฒด๋ ๋ค์๊ณผ ๊ฐ์ด ์ ์ฉํ ๋ฉ์๋๋ค์ ์ ๊ณตํฉ๋๋ค.
๋ฉ์๋ | ์ค๋ช |
add(...className) | - ์ธ์๋ก ์ ๋ฌํ 1๊ฐ ์ด์์ ๋ฌธ์์ด์ class ์ดํธ๋ฆฌ๋ทฐํธ ๊ฐ์ผ๋ก ์ถ๊ฐ |
remove(...className) | - ์ธ์๋ก ์ ๋ฌํ 1๊ฐ ์ด์์ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ ํด๋์ค๋ฅผ class ์ดํธ๋ฆฌ๋ทฐํธ์์ ์ญ์ - ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ ํด๋์ค๊ฐ class ์ดํธ๋ฆฌ๋ทฐํธ์ ์๋ค๋ฉด ์๋ฌ ์์ด ๋ฌด์ |
item(index) | - ์ธ์๋ก ์ ๋ฌํ index์ ํด๋นํ๋ ํด๋์ค๋ฅผ class ์ดํธ๋ฆฌ๋ทฐํธ์์ ๋ฐํ |
contain(...className) | - ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ ํด๋์ค๊ฐ class ์ดํธ๋ฆฌ๋ทฐํธ์ ํฌํจ๋์ด ์๋์ง ํ์ธ |
replace(oldClassName, newClassName) | - class ์ดํธ๋ฆฌ๋ทฐํธ์์ ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด์ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ |
toggle(className[,force] | - class ์ดํธ๋ฆฌ๋ทฐํธ์์ ์ธ์๋ก ์ ๋ฌํ ๋ฌธ์์ด๊ณผ ์ผ์นํ๋ ํด๋์ค๊ฐ ์กด์ฌํ๋ฉด ์ ๊ฑฐ, ์กด์ฌํ์ง ์์ผ๋ฉด ์ถ๊ฐ - ๋ ๋ฒ์งธ ์ธ์๋ก ๋ถ๋ฆฌ์ธ ๊ฐ์ผ๋ก ํ๊ฐ๋๋ ์กฐ๊ฑด์์ ์ ๋ฌํ ์ ์๋๋ฐ, ์ด๋ true๋ฉด class ์ดํธ๋ฆฌ๋ทฐํธ์ ๊ฐ์ ๋ก ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ฌธ์์ด์ ์ถ๊ฐํ๊ณ , flase๋ฉด ๊ฐ์ ๋ก ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ๋ฌธ์์ด์ ์ ๊ฑฐ |
์์์ ์ ์ฉ๋์ด ์๋ CSS ์คํ์ผ ์ฐธ์กฐ
HTML ์์์ ์ ์ฉ๋์ด ์๋ ๋ชจ๋ CSS ์คํ์ผ(์ธ๋ผ์ธ ์คํ์ผ ํฌํจ)์ ์ฐธ์กฐํด์ผ ํ ๊ฒฝ์ฐ getComputedStyle ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค.
window.getComputedStyle(element[,pseudo]) ๋ฉ์๋
- ์ฒซ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ์์ ๋ ธ๋์ ์ ์ฉ๋์ด ์๋ ํ๊ฐ๋ ์คํ์ผ์ CSSStyleDeclaration ๊ฐ์ฒด์ ๋ด์ ๋ฐํ
- ํ๊ฐ๋ ์คํ์ผ์ด๋ ์์ ๋ ธ๋์ ์ ์ฉ๋์ด ์๋ ๋ชจ๋ ์คํ์ผ (๋งํฌ ์คํ์ผ, ์๋ฒ ๋ฉ ์คํ์ผ, ์ธ๋ผ์ธ ์คํ์ผ, ์๋ฐ์คํฌ๋ฆฝํธ์์ ์ ์ฉํ ์คํ์ผ, ์์๋ ์คํ์ผ, ๊ธฐ๋ณธ ์คํ์ผ ๋ฑ)์ด ์กฐํฉ๋์ด ์ต์ข ์ ์ผ๋ก ์ ์ฉ๋ ์คํ์ผ
- ๋ ๋ฒ์งธ ์ธ์๋ก :after, :before ์ ๊ฐ์ ์์ฌ ์์๋ฅผ ์ง์ ํ๋ ๋ฌธ์์ด์ ์ ๋ฌํ ์ ์์ (์ต์ )
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: red;
}
.box {
width: 100px;
height: 50px;
background-color: cornsilk;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="box">Box</div>
<script>
const $box = document.querySelector('.box');
// .box ์์์ ์ ์ฉ๋ ๋ชจ๋ CSS ์คํ์ผ์ ๋ด๊ณ ์๋ CSSStyleDeclaration ๊ฐ์ฒด๋ฅผ ์ทจ๋
const computedStyle = window.getComputedStyle($box);
console.log(computedStyle); // CSSStyleDeclaration
// ์๋ฒ ๋ฉ ์คํ์ผ
console.log(computedStyle.width); // 100px
console.log(computedStyle.height); // 50px
console.log(computedStyle.backgroundColor); // rgb(255, 248, 220)
console.log(computedStyle.border); // 1px solid rgb(0, 0, 0)
// ์์ ์คํ์ผ(body -> .box)
console.log(computedStyle.color); // rgb(255, 0, 0)
// ๊ธฐ๋ณธ ์คํ์ผ
console.log(computedStyle.display); // block
// ์์ฌ ์์ :before์ ์คํ์ผ์ ์ทจ๋ํ๋ค.
const computedStyleBefore = window.getComputedStyle($box, ':before');
console.log(computedStyleBefore.content); // "Hello"
</script>
</body>
</html>
[์ถ์ฒ] ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive
๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive: ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ธฐ๋ณธ ๊ฐ๋ ๊ณผ ๋์ ์๋ฆฌ
269๊ฐ์ ๊ทธ๋ฆผ๊ณผ ์๋ฆฌ๋ฅผ ํํค์น๋ ์ค๋ช ์ผ๋ก โ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ธฐ๋ณธ ๊ฐ๋ ๊ณผ ๋์ ์๋ฆฌโ๋ฅผ ์ดํดํ์! ์นํ์ด์ง์ ๋จ์ํ ๋ณด์กฐ ๊ธฐ๋ฅ์ ์ฒ๋ฆฌํ๊ธฐ ์ํ ์ ํ์ ์ธ ์ฉ๋๋ก ํ์ด๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ ๊ณผ๋
wikibook.co.kr
'JavaScript > ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ Deep Dive' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] ํ์ด๋จธ (0) | 2022.08.13 |
---|---|
[JavaScript] ์ด๋ฒคํธ (0) | 2022.08.13 |
[JavaScript] ๋ธ๋ผ์ฐ์ ์ ๋ ๋๋ง ๊ณผ์ (0) | 2022.08.09 |
[JavaScript] Set๊ณผ Map (0) | 2022.08.07 |
[JavaScript] ๋์คํธ๋ญ์ฒ๋ง ํ ๋น (0) | 2022.08.06 |
- Total
- Today
- Yesterday
- Baekjoon
- ํฌํฌ์ธํฐ
- ํ๋กํผํฐ
- ๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ deep dive
- http
- map
- TDD
- ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ
- ์นด์นด์ค ์ธํด
- ๋ฐฑ์ค node.js
- fp
- ์๋ฐ
- ํ๋กํ ์ฝ
- ๋คํธ์ํฌ
- ํ๋ก๊ทธ๋๋จธ์ค
- 2019 ์นด์นด์ค ๊ฐ๋ฐ์ ๊ฒจ์ธ ์ธํด
- ์ฝ๋ฉํ ์คํธ
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ
- ์ด๋ถํ์
- ๋ฐฑ์ค
- ๋ค์ด๋๋ฏน ํ๋ก๊ทธ๋๋ฐ
- git
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์๊ณ ๋ฆฌ์ฆ
- ์ด์์ฒด์
- ๋์์ธ ํจํด
- ๋ ์์ปฌ ํ๊ฒฝ
- JavaScript
- ์ ์ญ ๋ณ์
- ๋ฐฑ์ค javascript
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |