Skip to content

Commit

Permalink
Merge pull request #1227 from nincoding/nincoding
Browse files Browse the repository at this point in the history
39-3์žฅ DOM - ํ•œ์ˆ˜์ง€
  • Loading branch information
Ryan-Dia authored Jan 18, 2024
2 parents 5cb5173 + 1ffe6cf commit 332d0b7
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions docs/39_DOM/ํ•œ์ˆ˜์ง€.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,83 @@ getElementsByClassName ๋ฉ”์„œ๋“œ๋Š” getElementsByTagName ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€
- `Document.prototype.getElementsByClassName` : DOM์˜ ๋ฃจํŠธ ๋…ธ๋“œ์ธ ๋ฌธ์„œ ๋…ธ๋“œ, ์ฆ‰ document๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœํ•˜๋ฉฐ **DOM ์ „์ฒด์—์„œ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜**ํ•ฉ๋‹ˆ๋‹ค.

- `Element.prototype.getElementsByClassName` : ํŠน์ • ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด ํ˜ธ์ถœํ•˜๋ฉฐ, **ํŠน์ • ์š”์†Œ ๋…ธ๋“œ์˜ ์ž์† ๋…ธ๋“œ ์ค‘์—์„œ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜**ํ•ฉ๋‹ˆ๋‹ค.

#### ๐Ÿ”Ž 2.4 CSS ์„ ํƒ์ž๋ฅผ ์ด์šฉํ•œ ์š”์†Œ ๋…ธ๋“œ ์ทจ๋“

> `CSS ์„ ํƒ์ž`(Selector)๋Š” **์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” HTML ์š”์†Œ๋ฅผ ํŠน์ •**ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฌธ๋ฒ•์ž…๋‹ˆ๋‹ค.
```css
/* ๋ชจ๋“  ์š”์†Œ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค */
* {
}

/* ํŠน์ • id ๊ฐ’์€ ํŠน์ • id์•ž์— #์„ ๋ถ™์ž…๋‹ˆ๋‹ค. */
#foo {
}

/* ํŠน์ • class ๊ฐ’์€ ํŠน์ • class์•ž์— .์„ ๋ถ™์ž…๋‹ˆ๋‹ค. */
.foo {
}
```

`Document.prototype/Element.prototype.querySelector` : ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ CSS ์„ ํƒ์ž๋ฅผ ๋งŒ์กฑ์‹œํ‚ค๋Š” **ํ•˜๋‚˜์˜ ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜**ํ•ฉ๋‹ˆ๋‹ค.

- ๋งŒ์•ฝ ์š”์†Œ ๋…ธ๋“œ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ ์ธ ๊ฒฝ์šฐ ์ฒซ๋ฒˆ์งธ๋กœ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
- ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
- ์„ ํƒ์ž๊ฐ€ ๋ฌธ๋ฒ•์— ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ, DOMException ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

```js
const elem = document.querySelector(".className");
```

`Document.prototype/Element.prototype.querySelectorAll` : ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ CSS ์„ ํƒ์ž๋ฅผ ๋งŒ์กฑ์‹œํ‚ค๋Š” **๋ชจ๋“  ์š”์†Œ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜**ํ•ฉ๋‹ˆ๋‹ค.

querySelectorAll ๋ฉ”์„œ๋“œ๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ์š”์†Œ ๋…ธ๋“œ ๊ฐ์ฒด๋ฅผ ๊ฐ–๋Š” `DOM ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด์ธ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜`ํ•ฉ๋‹ˆ๋‹ค. (์œ ์‚ฌ๋ฐฐ์—ด์ด๋ฉด์„œ ์ดํ„ฐ๋Ÿฌ๋ธ”)

- ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋นˆ NodeList ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
- ๋ฌธ๋ฒ•์— ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ, DOMException ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ

```js
// ul ์š”์†Œ์˜ ์ž์‹ ์š”์†Œ์ธ li ์š”์†Œ๋“ค์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜
const elem = document.querySelectorAll("ul > li");

// ๋ชจ๋“  ์š”์†Œ ๋…ธ๋“œ๋“ค์„ ํƒ์ƒ‰ํ•˜์—ฌ ๋ฐ˜ํ™˜
const all = document.querySelectorAll("*");
```

CSS ์„ ํƒ์ž ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” `getElementById` ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋ณด๋‹ค ๋‹ค์†Œ ๋Š๋ฆฌ๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

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

๋”ฐ๋ผ์„œ id ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•  ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด querySelector, querySelectorAll์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

#### ๐Ÿ”Ž 2.5 ํŠน์ • ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ

`Element.prototype.matches` : ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ **CSS ์„ ํƒ์ž๋ฅผ ํ†ตํ•ด ํŠน์ • ์š”์†Œ ๋…ธ๋“œ๋ฅผ ์ทจ๋“ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ**ํ•ฉ๋‹ˆ๋‹ค.

ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋Š” ์ด๋ฒคํŠธ ์œ„์ž„์„ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

#### ๐Ÿ”Ž 2.6 HTMLCollection๊ณผ NodeList

> **HTMLCollection๊ณผ NodeList์€ DOM API๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒฐ๊ณผ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์œ„ํ•œ DOM ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด**์ž…๋‹ˆ๋‹ค.
DOM ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด๋Š” ์œ ์‚ฌ๋ฐฐ์—ด์ด๋ฉด์„œ ์ดํ„ฐ๋Ÿฌ๋ธ”์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์Šคํ”„๋ ˆ๋“œ ๋ฌธ, for ...of, ๊ตฌ์กฐ๋ถ„ํ•ด์™€ ๊ฐ™์€ ๋ฌธ๋ฒ•๊ณผ ์ˆœํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

HTMLCollection๊ณผ NodeList์˜ ์ค‘์š”ํ•œ ํŠน์ง•์€ `์‚ด์•„์žˆ๋Š” ๊ฐ์ฒด` ๋ผ๋Š” ์ ์ž…๋‹ˆ๋‹ค.

HTMLCollection์€ ์–ธ์ œ๋‚˜ live ๊ฐ์ฒด๋กœ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

NodeList๋Š” ๋Œ€๋ถ€๋ถ„ ๋…ธ๋“œ ๊ฐ์ฒด ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ˜์˜ํ•˜์ง€ ์•Š๊ณ  ๊ณผ๊ฑฐ์˜ ์ •์  ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•˜๋Š” non-live ๊ฐ์ฒด๋กœ ๋™์ž‘ํ•˜์ง€๋งŒ ๊ฒฝ์šฐ์— ๋”ฐ๋ผ live๊ฐ์ฒด๋กœ ๋™์ž‘ํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

- HTMLCollection : **๋…ธ๋“œ ๊ฐ์ฒด ์ƒํƒœ ๋ณ€ํ™”๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋ฐ˜์˜**ํ•˜๋Š” **์‚ด์•„์žˆ๋Š”(live) DOM ์ปฌ๋ ‰์…˜ ๊ฐ์ฒด**์ž…๋‹ˆ๋‹ค.

- getElementsByTagName, getElementsByClassName ๋“ฑ...
- HTMLCollection์€ ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋…ธ๋“œ ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋ณ€๊ฒฝํ•˜์—ฌ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๊ธฐ์— ๋ถ€์ž‘์šฉ์ด ์žˆ๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

- NodeList : ์‹ค์‹œ๊ฐ„์œผ๋กœ ๋…ธ๋“œ ๊ฐ์ฒด์˜ ์ƒํƒœ ๋ณ€๊ฒฝ์„ ๋ฐ˜์˜ํ•˜์ง€ ์•Š๋Š”(non-live) ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

- querySelectorAll
- HTMLCollection ๊ฐ์ฒด์˜ ๋ถ€์ž‘์šฉ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํ•จ
- ๊ฒฝ์šฐ์— ๋”ฐ๋ผ live๊ฐ์ฒด๋กœ ๋™์ž‘ํ•˜๊ธฐ๋„ ํ•จ์œผ๋กœ ์ฃผ์˜๊ฐ€ ํ•„์š”

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

0 comments on commit 332d0b7

Please sign in to comment.