Skip to content

Commit

Permalink
Merge pull request #1236 from SebellKo/sebellko
Browse files Browse the repository at this point in the history
๏ฟฝ39-4์žฅ DOM - ๊ณ ์„ธ์ข…
  • Loading branch information
Ryan-Dia authored Jan 19, 2024
2 parents 4065dee + 300dc82 commit 7a994a3
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/39_DOM/๊ณ ์„ธ์ข….md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,49 @@ HTMLCollection ๊ฐ์ฒด์˜ ๋ถ€์ž‘์šฉ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด `querySelectorAll` ๋ฉ”
NodeList๋Š” non-live ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋Ÿฐ ๋ถ€์ž‘์šฉ์„ ์˜ˆ๋ฐฉํ•ด ์ฃผ์ง€๋งŒ `childNode` ํ”„๋กœํผํ‹ฐ๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” NodeList ๊ฐ์ฒด๋Š” HTMLCollection ๊ฐ์ฒด์™€ ๊ฐ™์ด ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋…ธ๋“œ ๊ฐ์ฒด์˜ ์ƒํƒœ ๋ณ€๊ฒฝ์„ ๋ฐ˜์˜ํ•˜๋Š” live ๊ฐ์ฒด๋กœ ๋™์ž‘ํ•˜๋ฏ€๋กœ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

**๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ ์˜ˆ์ƒ๊ณผ ๋‹ค๋ฅธ ๋™์ž‘์„ ํ”ผํ•˜๊ณ  ์˜ˆ๋ฐฉํ•˜๊ธฐ ์œ„ํ•ด์„  ๋…ธ๋“œ ๊ฐ์ฒด์˜ ์ƒํƒœ ๋ณ€๊ฒฝ๊ณผ ์ƒ๊ด€์—†์ด ์•ˆ์ „ํ•˜๊ฒŒ DOM ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด HTMLCollection์ด๋‚˜ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.**

---

## ๋…ธ๋“œํƒ์ƒ‰

์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•˜๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๊ธฐ์ ์œผ๋กœ DOM ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ๋ฅผ ์˜ฎ๊ฒจ ๋‹ค๋‹ˆ๋ฉฐ ๋ถ€๋ชจ, ํ˜•์ œ, ์ž์‹ ๋…ธ๋“œ ๋“ฑ์„ ํƒ์ƒ‰ ํ•ด์•ผ ํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.<br>
์ด๋ฅผ ์œ„ํ•ด์„œ `Node, Element` ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํŠธ๋ฆฌ ํƒ์ƒ‰ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

`parentNode, previousSibling, firstChild, childNodes` ํ”„๋กœํผํ‹ฐ๋Š” `Node.prototype`์ด ์ œ๊ณตํ•˜๊ณ , `previesElementSibling, nextElementSibling, children`ํ”„๋กœํผํ‹ฐ๋Š” `Element.prototype`์ด ์ œ๊ณตํ•œ๋‹ค.

### ๊ณต๋ฐฑ ํ…์ŠคํŠธ ๋…ธ๋“œ

HTML ์š”์†Œ ์‚ฌ์ด์˜ ์ŠคํŽ˜์ด์Šค, ํƒญ, ์ค„๋ฐ”๊ฟˆ ๋“ฑ์˜ ๊ณต๋ฐฑ ๋ฌธ์ž๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.<br>
์ด๋ฅผ ๊ณต๋ฐฑ ํ…์ŠคํŠธ ๋…ธ๋“œ๋ผ๊ณ  ํ•œ๋‹ค.

```
<ul><๊ณต๋ฐฑํ…์ŠคํŠธ๋…ธ๋“œ>
<li></li><๊ณต๋ฐฑํ…์ŠคํŠธ๋…ธ๋“œ>
<li></li><๊ณต๋ฐฑํ…์ŠคํŠธ๋…ธ๋“œ>
<li></li><๊ณต๋ฐฑํ…์ŠคํŠธ๋…ธ๋“œ>
</ul>
```

์œ„ ์˜ˆ์ œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋“ฏ์ด ๊ณต๋ฐฑ ํ…์ŠคํŠธ ๋…ธ๋“œ๋„ ํ•˜๋‚˜์˜ ๋…ธ๋“œ๋กœ์„œ DOM ํŠธ๋ฆฌ์— ํฌํ•จ๋˜๊ฒŒ ๋œ๋‹ค.<br>
๋”ฐ๋ผ์„œ, ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ๋Š” ๊ณต๋ฐฑ ๋ฌธ์ž๊ฐ€ ์ƒ์„ฑํ•œ ๊ณต๋ฐฑ ํ…์ŠคํŠธ ๋…ธ๋“œ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

### ์ž์‹ ๋…ธ๋“œ ํƒ์ƒ‰

์ž์‹ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ”„๋กœํผํ‹ฐ๋ฅด ์‚ฌ์šฉํ•œ๋‹ค.

1. `Node.prototype.childNodes` : `NodeList`๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ…์ŠคํŠธ ๋…ธ๋“œ๋„ ํฌํ•จ๋˜์–ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.
2. `Element.prototype.children` : `HTMLCollection`์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ, ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.
3. `Node.prototype.firstChilde` : ํ…์ŠคํŠธ ๋…ธ๋“œ์ด๊ฑฐ๋‚˜ ์š”์†Œ ๋…ธ๋“œ์ด๋‹ค.
4. `Node.prototype.lastChild` : ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ํ…์ŠคํŠธ ๋…ธ๋“œ์ด๊ฑฐ๋‚˜ ์š”์†Œ ๋…ธ๋“œ์ด๋‹ค.
5. `Element.prototype.firstElementChild` : ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
6. `Element.prototype.lastElementChilde` : ๋งˆ์ง€๋ง‰ ์ž์‹ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์š”์†Œ ๋…ธ๋“œ๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

### ์ž์‹ ๋…ธ๋“œ ์กด์žฌ ํ™•์ธ

์ž์‹ ๋…ธ๋“œ์˜ ์กด์žฌ ํ™•์ธ์œผ๋กœ `Node.prototype.hasChildNodes` ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋˜์ง€๋งŒ, ์ด๊ฒฝ์šฐ ํ…์ŠคํŠธ ๋…ธ๋“œ๊นŒ์ง€ ํฌํ•จํ•˜์—ฌ ๊ฒ€์‚ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—<br> `children.length` ๋˜๋Š” `Element` ์ธํ„ฐํŽ˜์ด์Šค์˜ `childElementCount` ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜์ž.

๋ถ€๋ชจ ๋…ธ๋“œ ํƒ์ƒ‰์—๋Š” `Node.prototype.parentNode`ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ๋ถ€๋ชจ ๋…ธ๋“œ๋Š” ์ข…๋‹จ ๋…ธ๋“œ์ธ ๋ฆฌํ”„ ๋…ธ๋“œ ์ด๋ฏ€๋กœ ํ…์ŠคํŠธ ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

### ํ˜•์ œ ๋…ธ๋“œ ํƒ์ƒ‰

ํ˜•์ œ ๋…ธ๋“œ ํƒ์ƒ‰์—๋Š” `Node.prototype.previousSibling, Node.prototype.nextSibling, Element.prototype.previousElementSibling, Element.prototype.nextElementSibling` ํ”„๋กœํผํ‹ฐ๊ฐ€ ์‚ฌ์šฉ๋˜๋ฉฐ, Node ํ”„๋กœํ† ํƒ€์ž… ํ”„๋กœํผํ‹ฐ๋Š” ํ…์ŠคํŠธ ๋…ธ๋“œ๊ฐ€ ๋ฐ˜ํ™˜๋  ์ˆ˜ ๋„ ์žˆ๋‹ค.

0 comments on commit 7a994a3

Please sign in to comment.