Skip to content

Commit

Permalink
Merge pull request #1221 from narinn-star/narinn-star
Browse files Browse the repository at this point in the history
39-2μž₯ DOM - μ΄λ‚˜λ¦°
  • Loading branch information
Ryan-Dia authored Jan 17, 2024
2 parents aaa7e1b + efacf5d commit 18c998e
Showing 1 changed file with 122 additions and 1 deletion.
123 changes: 122 additions & 1 deletion docs/39_DOM/μ΄λ‚˜λ¦°.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 🎯 39 DOM

[39.1 λ…Έλ“œ](#1-λ…Έλ“œ)
[39.1 λ…Έλ“œ](#1-λ…Έλ“œ)
[39.2 μš”μ†Œ λ…Έλ“œ 취득](#2-μš”μ†Œ-λ…Έλ“œ-취득)

## 0. DOM

Expand Down Expand Up @@ -96,3 +97,123 @@ HTML μš”μ†Œκ°€ κ°μ²΄ν™”λœ μš”μ†Œ λ…Έλ“œ κ°μ²΄λŠ” HTML μš”μ†Œκ°€ κ°–λŠ” 곡톡
이처럼 λ…Έλ“œ κ°μ²΄λŠ” κ³΅ν†΅λœ κΈ°λŠ₯일수둝 ν”„λ‘œν† νƒ€μž… 체인의 μƒμœ„μ—, κ°œλ³„μ μΈ 고유 κΈ°λŠ₯일수둝 ν”„λ‘œν† νƒ€μž… 체인의 ν•˜μœ„μ— ν”„λ‘œν† νƒ€μž… 체인을 κ΅¬μΆ•ν•˜μ—¬ λ…Έλ“œ 객체에 ν•„μš”ν•œ κΈ°λŠ₯, 즉 ν”„λ‘œνΌν‹°μ™€ λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•˜λŠ” 상속 ꡬ쑰λ₯Ό κ°–λŠ”λ‹€.

πŸ‘‰πŸ» DOM은 HTML λ¬Έμ„œμ˜ 계측적 ꡬ쑰와 정보λ₯Ό ν‘œν˜„ν•˜λŠ” 것은 λ¬Όλ‘  λ…Έλ“œ 객체의 μ’…λ₯˜, 즉 λ…Έλ“œ νƒ€μž…μ— 따라 ν•„μš”ν•œ κΈ°λŠ₯을 ν”„λ‘œνΌν‹°μ™€ λ©”μ„œλ“œμ˜ 집합인 DOM API둜 μ œκ³΅ν•œλ‹€. 이 DOM APIλ₯Ό 톡해 HTML의 κ΅¬μ‘°λ‚˜ λ‚΄μš© λ˜λŠ” μŠ€νƒ€μΌ 등을 λ™μ μœΌλ‘œ μ‘°μž‘ν•  수 μžˆλ‹€.

## 2. μš”μ†Œ λ…Έλ“œ 취득

HTML의 κ΅¬μ‘°λ‚˜ λ‚΄μš© λ˜λŠ” μŠ€νƒ€μΌ 등을 λ™μ μœΌλ‘œ μ‘°μž‘ν•˜λ €λ©΄ μš”μ†Œ λ…Έλ“œλ₯Ό μ·¨λ“ν•΄μ•Όν•œλ‹€.

HTML λ¬Έμ„œ λ‚΄μ˜ h1 μš”μ†Œμ˜ ν…μŠ€νŠΈλ₯Ό λ³€κ²½ν•˜κ³  싢은 경우, λ¨Όμ € DOM 트리 내에 μ‘΄μž¬ν•˜λŠ” h1 μš”μ†Œ λ…Έλ“œλ₯Ό 취득할 ν•„μš”κ°€ μžˆλ‹€. μ·¨λ“ν•œ μš”μ†Œ λ…Έλ“œμ˜ μžμ‹ λ…Έλ“œμΈ ν…μŠ€νŠΈ λ…Έλ“œλ₯Ό λ³€κ²½ν•˜λ©΄ h1 μš”μ†Œμ˜ ν…μŠ€νŠΈκ°€ λ³€κ²½λœλ‹€.

πŸ‘‰πŸ» μš”μ†Œ λ…Έλ“œμ˜ 취득은 HTML μš”μ†Œλ₯Ό μ‘°μž‘ν•˜λŠ” μ‹œμž‘μ μ΄λ‹€. 이λ₯Ό μœ„ν•΄ DOM은 μš”μ†Œ λ…Έλ“œλ₯Ό 취득할 수 μžˆλŠ” λ‹€μ–‘ν•œ λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•œλ‹€.

### 2.1 idλ₯Ό μ΄μš©ν•œ μš”μ†Œ λ…Έλ“œ 취득

Document.prototype.getElementById λ©”μ„œλ“œλŠ” 인수둜 μ „λ‹¬ν•œ id 값을 κ°–λŠ” ν•˜λ‚˜μ˜ μš”μ†Œ λ…Έλ“œλ₯Ό νƒμƒ‰ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€. λ°˜λ“œμ‹œ λ¬Έμ„œ λ…Έλ“œμΈ documentλ₯Ό 톡해 ν˜ΈμΆœν•΄μ•Ό ν•œλ‹€.

```html
<!DOCTYPE html>
<html>
<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>
const $elem = document.getElementById('banana');
$elem.style.color = 'red';
</script>
</body>
</html>
```

id 값은 HTML λ¬Έμ„œ λ‚΄μ—μ„œ μœ μΌν•œ 값이어야 ν•œλ‹€. 단, μ€‘λ³΅λœ id 값을 κ°–λŠ” HTML μš”μ†Œκ°€ μ—¬λŸ¬ 개 μ‘΄μž¬ν•˜λ”λΌλ„ μ–΄λ–€ μ—λŸ¬λ„ λ°œμƒν•˜μ§€ μ•ŠλŠ”λ‹€. κ·ΈλŸ¬ν•œ 경우, getElementById λ©”μ„œλ“œλŠ” 인수둜 μ „λ‹¬λœ id 값을 κ°–λŠ” 첫 번째 μš”μ†Œ λ…Έλ“œλ§Œ λ°˜ν™˜ν•œλ‹€. (ν•˜λ‚˜λ§Œ λ°˜ν™˜)

λ§Œμ•½ 인수둜 μ „λ‹¬λœ id 값이 μ—†λ‹€λ©΄ getElementById λ©”μ„œλ“œλŠ” null을 λ°˜ν™˜ν•œλ‹€.

HTML μš”μ†Œμ— id μ–΄νŠΈλ¦¬λ·°νŠΈλ₯Ό λΆ€μ—¬ν•˜λ©΄ id κ°’κ³Ό λ™μΌν•œ μ΄λ¦„μ˜ μ „μ—­ λ³€μˆ˜κ°€ μ•”λ¬΅μ μœΌλ‘œ μ„ μ–Έλ˜κ³  ν•΄λ‹Ή λ…Έλ“œ 객체가 ν• λ‹Ήλ˜λŠ” λΆ€μˆ˜ νš¨κ³Όκ°€ μžˆλ‹€.

```html
<!DOCTYPE html>
<html>
<body>
<div id="foo"></div>
<script>
console.log(foo === document.getElementById('foo'));
delete foo;
console.log(foo);
</script>
</body>
</html>
```

idκ°’κ³Ό λ™μΌν•œ μ΄λ¦„μ˜ μ „μ—­ λ³€μˆ˜κ°€ 이미 μ„ μ–Έλ˜μ–΄ 있으면 이 μ „μ—­ λ³€μˆ˜μ— λ…Έλ“œ 객체가 μž¬ν• λ‹Ήλ˜μ§€ μ•ŠλŠ”λ‹€.

### 2.2 νƒœκ·Έ 이름을 μ΄μš©ν•œ μš”μ†Œ λ…Έλ“œ 취득

Document.prototype/Element.prototype.getElementsByTagName λ©”μ„œλ“œλŠ” 인수둜 μ „λ‹¬ν•œ νƒœκ·Έ 이름을 κ°–λŠ” λͺ¨λ“  μš”μ†Œ λ…Έλ“œλ“€μ„ νƒμƒ‰ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€. μ—¬λŸ¬ 개의 μš”μ†Œ λ…Έλ“œ 객체λ₯Ό κ°–λŠ” DOM μ»¬λ ‰μ…˜ 객체인 HTMLCollection 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.

ν•¨μˆ˜λŠ” ν•˜λ‚˜μ˜ κ°’λ§Œ λ°˜ν™˜ν•  수 μžˆμœΌλ―€λ‘œ μ—¬λŸ¬ 개의 값을 λ°˜ν™˜ν•˜λ €λ©΄ λ°°μ—΄μ΄λ‚˜ 객체와 같은 μžλ£Œκ΅¬μ‘°μ— λ‹΄μ•„ λ°˜ν™˜ν•΄μ•Ό ν•œλ‹€. getElementsByTagName λ©”μ„œλ“œκ°€ λ°˜ν™˜ν•˜λŠ” DOM μ»¬λ ‰μ…˜ 객체인 HTMLCollection κ°μ²΄λŠ” μœ μ‚¬ λ°°μ—΄ κ°μ²΄μ΄λ©΄μ„œ μ΄ν„°λŸ¬λΈ”μ΄λ‹€.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
<li id="apple">Apple</li>
<li id="banana">Banana</li>
<li id="orange">Orange</li>
</ul>
<script>
const $elem = document.getElementsByTagName('li');
[...$elems].forEach((elem) => {
elem.style.color = 'red';
});
</script>
</body>
</html>
```

Element.prototype.getElementsByTagName λ©”μ„œλ“œλŠ” νŠΉμ • μš”μ†Œ λ…Έλ“œλ₯Ό 톡해 ν˜ΈμΆœν•˜λ©°, νŠΉμ • μš”μ†Œ λ…Έλ“œμ˜ μžμ† λ…Έλ“œ μ€‘μ—μ„œ μš”μ†Œ λ…Έλ“œλ₯Ό νƒμƒ‰ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€.

λ§Œμ•½ 인수둜 μ „λ‹¬λœ νƒœκ·Έ 이름을 κ°–λŠ” μš”μ†Œκ°€ μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 경우 getElementsByTagName λ©”μ„œλ“œλŠ” 빈 HTMLCollection 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.

### 2.3 classλ₯Ό μ΄μš©ν•œ μš”μ†Œ λ…Έλ“œ 취득

Document.prototype/Element.prototype.getElementByClassName λ©”μ„œλ“œλŠ” 인수둜 μ „λ‹¬ν•œ class 값을 κ°–λŠ” λͺ¨λ“  μš”μ†Œλ“€μ„ νƒμƒ‰ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€. 곡백으둜 ꡬ뢄해 μ—¬λŸ¬ 개의 classλ₯Ό 지정할 수 있으며 μ—¬λŸ¬ 개의 μš”μ†Œ λ…Έλ“œ 객체λ₯Ό κ°–λŠ” DOM μ»¬λ ‰μ…˜ 객체인 HTMLCollection 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.

```html
<!DOCTYPE html>
<html>
<body>
<ul>
<li class="fruit apple">Apple</li>
<li class="fruit banana">Banana</li>
<li class="fruit orange">Orange</li>
</ul>
<script>
const $elems = document.getElementsByClassName('fruit');
const $apples = document.getElementsByClassName('fruit apple');
[...$apples].forEach((elem) => {
elem.style.color = 'blue';
});
</script>
</body>
</html>
```

Document.prototype.getElementsByClassName λ©”μ„œλ“œλŠ” DOM의 루트 λ…Έλ“œμΈ λ¬Έμ„œ λ…Έλ“œ 즉, documentλ₯Ό 톡해 ν˜ΈμΆœν•˜λ©° DOM μ „μ²΄μ—μ„œ μš”μ†Œ λ…Έλ“œλ₯Ό νƒμƒ‰ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€.

Element.prototype.getElementsByClassName λ©”μ„œλ“œλŠ” νŠΉμ • μš”μ†Œ λ…Έλ“œλ₯Ό 톡해 ν˜ΈμΆœν•˜λ©° νŠΉμ • μš”μ†Œ λ…Έλ“œμ˜ μžμ† λ…Έλ“œ μ€‘μ—μ„œ μš”μ†Œ λ…Έλ“œλ₯Ό νƒμƒ‰ν•˜μ—¬ λ°˜ν™˜ν•œλ‹€.

λ§Œμ•½ 인수둜 μ „λ‹¬λœ class 값을 κ°–λŠ” μš”μ†Œκ°€ 없을 경우, 빈 HTMLCollection 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.

0 comments on commit 18c998e

Please sign in to comment.