Skip to content

Commit

Permalink
docs: DOM (39.1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccconac committed Jan 15, 2024
1 parent ead8d65 commit a4d3a67
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions docs/39_DOM/๋ฌธ์†Œํฌ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# ๐Ÿš€ DOM

## ๐Ÿ”Ž 1. ๋…ธ๋“œ

### ๐Ÿ’ญ 1. HTML ์š”์†Œ์™€ ๋…ธ๋“œ ๊ฐ์ฒด

> HTML ์š”์†Œ<sup>HTML element</sup>๋Š” HTML ๋ฌธ์„œ๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๊ฐœ๋ณ„์ ์ธ ์š”์†Œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. HTML ์š”์†Œ๋Š” ๋ Œ๋”๋ง ์—”์ง„์— ์˜ํ•ด ํŒŒ์‹ฑ๋˜์–ด DOM์„ ๊ตฌ์„ฑํ•˜๋Š” ์š”์†Œ ๋…ธ๋“œ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜๋œ๋‹ค.
HTML ๋ฌธ์„œ๋Š” HTML ์š”์†Œ๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋ฉฐ, HTML ์š”์†Œ๋Š” ์ค‘์ฒฉ ๊ด€๊ณ„๋ฅผ ๊ฐ–๋Š”๋‹ค. ์ด๋กœ ์ธํ•ด HTML ์š”์†Œ ๊ฐ„์—๋Š” ๊ณ„์ธต์ ์ธ ๋ถ€์ž ๊ด€๊ณ„<sup>parent-child</sup>๊ฐ€ ํ˜•์„ฑ๋˜๋ฉฐ, ๋ถ€์ž ๊ด€๊ณ„๋ฅผ ๋ฐ˜์˜ํ•ด HTML ์š”์†Œ๋ฅผ ๊ฐ์ฒดํ™”ํ•œ ๋ชจ๋“  ๋…ธ๋“œ ๊ฐ์ฒด๋“ค์„ ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค.

![HTML](https://velog.velcdn.com/images/bluecoolgod80/post/82d3b1c6-4bb2-4132-b751-64298a874170/image.png)

![HTML-Node](https://velog.velcdn.com/images/bluecoolgod80/post/3a9a86c7-1b63-4549-a3e3-12d227b2d91e/image.png)

#### โœจ ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ

> **๋…ธ๋“œ ๊ฐ์ฒด๋“ค๋กœ ๊ตฌ์„ฑ๋œ ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ DOM<sup>Document Object Model</sup>์ด๋ผ๊ณ  ํ•˜๋ฉฐ,** ๋…ธ๋“œ ๊ฐ์ฒด์˜ ํŠธ๋ฆฌ๋กœ ๊ตฌ์กฐํ™”๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— DOM์„ **DOM ํŠธ๋ฆฌ๋ผ๊ณ ** ๋ถ€๋ฅด๊ธฐ๋„ ํ•œ๋‹ค.
์—ฌ๊ธฐ์„œ ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ<sup>tree data structure</sup>๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ<sup>parent node</sup>์™€ ์ž์‹ ๋…ธ๋“œ<sup>child node</sup>๋กœ ๊ตฌ์„ฑ๋˜์–ด ๋…ธ๋“œ ๊ฐ„์˜ ๊ณ„์ธต์  ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋น„์„ ํ˜• ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋งํ•œ๋‹ค.

### ๐Ÿ’ญ 2. ๋…ธ๋“œ ๊ฐ์ฒด์˜ ํƒ€์ž…

```html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="style.css" />
</head>
<body>
<ul>
<li id="apple">Apple</li>
<li id="banana">Banana</li>
<li id="orange">Orange</li>
</ul>
<script src="app.js"></script>
</body>
</html>
```

๋ Œ๋”๋ง ์—”์ง„์€ ์œ„ HTML ๋ฌธ์„œ๋ฅผ ํŒŒ์‹ฑํ•ด ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์€ DOM์„ ์ƒ์„ฑํ•œ๋‹ค. ์ด์ฒ˜๋Ÿผ DOM์€ ๋…ธ๋“œ ๊ฐ์ฒด์˜ ๊ณ„์ธต์ ์ธ ๊ตฌ์กฐ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๋…ธ๋“œ ๊ฐ์ฒด์—๋Š” ์ข…๋ฅ˜๊ฐ€ ์žˆ๊ณ  ์ƒ์† ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง„๋‹ค.

![](https://velog.velcdn.com/images/bluecoolgod80/post/3eb5d3da-5551-4b51-ae28-7206c67f51d5/image.png)

๋…ธ๋“œ ๊ฐ์ฒด๋Š” ์ด 12๊ฐœ์˜ ์ข…๋ฅ˜<sup>node type</sup>๊ฐ€ ์žˆ๋‹ค. ์ด ์ค‘์—์„œ๋„ ์ค‘์š”ํ•œ ๋…ธ๋“œ ํƒ€์ž… ๋„ค ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ ์ข…๋ฅ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค. ๐Ÿ‘€

#### โœจ ๋ฌธ์„œ ๋…ธ๋“œ<sup>document node</sup>

> ๋ฌธ์„œ ๋…ธ๋“œ๋Š” **DOM ํŠธ๋ฆฌ์˜ ์ตœ์ƒ์œ„์— ์กด์žฌํ•˜๋Š” ๋ฃจํŠธ ๋…ธ๋“œ๋กœ์„œ document ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.** document ๊ฐ์ฒด๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ Œ๋”๋งํ•œ HTML ๋ฌธ์„œ ์ „์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, ์ „์—ญ ๊ฐ์ฒด window์˜ document ํ”„๋กœํผํ‹ฐ์— ๋ฐ”์ธ๋”ฉ๋˜์–ด ์žˆ๋‹ค. _๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์˜ ๋ชจ๋“  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋Š” ํ•˜๋‚˜์˜ ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ณต์œ ํ•˜๋ฏ€๋กœ HTML ๋ฌธ์„œ๋‹น document ๊ฐ์ฒด๋Š” ์œ ์ผํ•˜๋‹ค._
๋ฌธ์„œ ๋…ธ๋“œ๋Š” DOM ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ๋…ธ๋“œ์ด๋ฏ€๋กœ DOM ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋“ค์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ง„์ž…์ <sup>entry point</sup> ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์š”์†Œ, ์–ดํŠธ๋ฆฌ๋ทฐํŠธ, ํ…์ŠคํŠธ ๋…ธ๋“œ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ๋ฌธ์„œ ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด์•ผ ํ•œ๋‹ค.

#### โœจ ์š”์†Œ ๋…ธ๋“œ<sup>element node</sup>

> ์š”์†Œ ๋…ธ๋“œ๋Š” **HTML ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋‹ค.** ์š”์†Œ ๋…ธ๋“œ๋Š” HTML ๊ฐ„์˜ ์ค‘์ฒฉ์— ์˜ํ•ด ๋ถ€์ž ๊ด€๊ณ„๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์ด ๋ถ€์ž ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ์ •๋ณด๋ฅผ ๊ตฌ์กฐํ™”ํ•˜๋ฏ€๋กœ _์š”์†Œ ๋…ธ๋“œ๋Š” ๋ฌธ์„œ์˜ ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„ํ•œ๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค._
#### โœจ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œ<sup>attribute node</sup>

> ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œ๋Š” **HTML ์š”์†Œ์˜ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋กœ,** ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๊ฐ€ ์ง€์ •๋œ HTML ์š”์†Œ์˜ ์š”์†Œ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค.
์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œ๋Š” ๋ถ€๋ชจ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š๊ณ  ์š”์†Œ ๋…ธ๋“œ์—๋งŒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด ๋ถ€๋ชจ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ, ์š”์†Œ ๋…ธ๋“œ์˜ ํ˜•์ œ ๋…ธ๋“œ<sup>sibling node</sup>๋Š” ์•„๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œ์— ์ ‘๊ทผํ•ด ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ฐธ์กฐ/๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ๋จผ์ € ์š”์†Œ ๋…ธ๋“œ์— ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค! ๐Ÿ‘€

#### โœจ ํ…์ŠคํŠธ ๋…ธ๋“œ<sup>text node</sup>

> ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” **HTML ์š”์†Œ์˜ ํ…์ŠคํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋กœ,** _๋ฌธ์„œ์˜ ์ •๋ณด๋ฅผ_ ํ‘œํ˜„ํ•œ๋‹ค.
ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์‹์ด๋ฉฐ, ์ž์‹ ๋…ธ๋“œ๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋Š” ๋ฆฌํ”„ ๋…ธ๋“œ<sup>leaf node</sup>๋‹ค. ์ฆ‰, ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” DOM ํŠธ๋ฆฌ์˜ ์ตœ์ข…๋‹จ์œผ๋กœ, ํ…์ŠคํŠธ ๋…ธ๋“œ์— ์ ‘๊ทผํ•˜๋ ค๋ฉด ๋จผ์ € ๋ถ€๋ชจ ๋…ธ๋“œ์ธ ์š”์†Œ ๋…ธ๋“œ์— ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค.

### ๐Ÿ’ญ 3. ๋…ธ๋“œ ๊ฐ์ฒด์˜ ์ƒ์† ๊ตฌ์กฐ

> **DOM์€ HTML ๋ฌธ์„œ์˜ ๊ณ„์ธต์  ๊ตฌ์กฐ์™€ ์ •๋ณด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋ฌผ๋ก  ๋…ธ๋“œ ๊ฐ์ฒด์˜ ์ข…๋ฅ˜์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์„œ๋“œ์˜ ์ง‘ํ•ฉ์ธ DOM API<sup>Application Programming Interface</sup>๋กœ ์ œ๊ณตํ•œ๋‹ค. ์ด DOM API๋ฅผ ํ†ตํ•ด HTML์˜ ๊ตฌ์กฐ๋‚˜ ๋‚ด์šฉ ๋˜๋Š” ์Šคํƒ€์ผ ๋“ฑ์„ ๋™์ ์œผ๋กœ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.**
DOM์„ ๊ตฌ์„ฑํ•˜๋Š” ๋…ธ๋“œ ๊ฐ์ฒด๋Š” ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” ํ˜ธ์ŠคํŠธ ๊ฐ์ฒด<sup>host objects</sup>์ง€๋งŒ, ๋…ธ๋“œ ๊ฐ์ฒด๋„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด์ด๋ฏ€๋กœ ํ”„๋กœํ† ํƒ€์ž…์— ์˜ํ•œ ์ƒ์† ๊ตฌ์กฐ๋ฅผ ๊ฐ–๋Š”๋‹ค.

๋ชจ๋“  ๋…ธ๋“œ ๊ฐ์ฒด๋Š” Object, EventTarge, Node ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ ๋ฌธ์„œ ๋…ธ๋“œ๋Š” `Document`, `HTMLDocument`๋ฅผ, ์–ดํŠธ๋ฆฌ๋ทฐํŠธ ๋…ธ๋“œ๋Š” `Attr`, ํ…์ŠคํŠธ ๋…ธ๋“œ๋Š” `CharacterData` ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ๊ฐ ์ƒ์†๋ฐ›๋Š”๋‹ค. ์š”์†Œ ๋…ธ๋“œ๋Š” `Element` ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์œผ๋ฉฐ, ์ถ”๊ฐ€์ ์œผ๋กœ ์„ธ๋ถ„ํ™”๋œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ๐Ÿ‘€

![](https://velog.velcdn.com/images/bluecoolgod80/post/68278ac2-2a08-4865-9792-4cdb7a69de13/image.png)

input ์š”์†Œ ๋…ธ๋“œ ๊ฐ์ฒด๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์— ์žˆ๋Š” ๋ชจ๋“  ํ”„๋กœํ† ํƒ€์ž…์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์ด ๊ฐ์ฒด์ธ ๋™์‹œ์— ๋ฐฐ์—ด์ธ ๊ฒƒ์ฒ˜๋Ÿผ input ์š”์†Œ ๋…ธ๋“œ ๊ฐ์ฒด๋„ ๋‹ค์–‘ํ•œ ํŠน์„ฑ์„ ๊ฐ–๋Š” ๊ฐ์ฒด์ด๋ฉฐ, ์ด๋Ÿฌํ•œ ํŠน์„ฑ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ธฐ๋Šฅ๋“ค์„ ์ƒ์†์„ ํ†ตํ•ด ์ œ๊ณต๋ฐ›๋Š”๋‹ค.

![](https://velog.velcdn.com/images/bluecoolgod80/post/9239da6e-1d54-4b17-8467-657673c53fc2/image.png)

| input ์š”์†Œ ๋…ธ๋“œ ๊ฐ์ฒด์˜ ํŠน์„ฑ | ํ”„๋กœํ† ํƒ€์ž…์„ ์ œ๊ณตํ•˜๋Š” ๊ฐ์ฒด |
| -------------------------------------------------------------------------- | -------------------------- |
| ๊ฐ์ฒด | Object |
| ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ฐ์ฒด | EventTarget |
| ํŠธ๋ฆฌ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๋…ธ๋“œ ๊ฐ์ฒด | Node |
| ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ๋Š” ์›น ๋ฌธ์„œ์˜ ์š”์†Œ(HTML, XML, SVG)๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ์ฒด | Element |
| ์›น ๋ฌธ์„œ์˜ ์š”์†Œ ์ค‘์—์„œ HTML ์š”์†Œ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ์ฒด | HTMLElement |
| HTML ์š”์†Œ ์ค‘์—์„œ input ์š”์†Œ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๊ฐ์ฒด | HTMLInputElement |

## ๐Ÿ‘€ REFERENCE

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive 39์žฅ DOM

0 comments on commit a4d3a67

Please sign in to comment.