Skip to content

Commit

Permalink
docs: 39.2.1 ~ 39.2.3 정리
Browse files Browse the repository at this point in the history
  • Loading branch information
e6d1fe committed Jan 16, 2024
1 parent 2b2c341 commit 067b9a0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/39_DOM/λ‚˜μ„Έν˜„.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,43 @@ DOM을 κ΅¬μ„±ν•˜λŠ” λ…Έλ“œ κ°μ²΄λŠ” μžμ‹ μ˜ ꡬ쑰와 정보λ₯Ό μ œμ–΄ν•  수
λ…Έλ“œ κ°μ²΄μ—λŠ” λ…Έλ“œ 객체의 μ’…λ₯˜μ™€ 상관없이 λͺ¨λ‘ κ³΅ν†΅μœΌλ‘œ κ°–λŠ” κΈ°λŠ₯도 있고, νƒ€μž…μ— 따라 κ³ μœ ν•œ κΈ°λŠ₯도 μžˆλ‹€.
예λ₯Ό λ“€μ–΄, λͺ¨λ“  λ…Έλ“œ κ°μ²΄λŠ” κ³΅ν†΅μ μœΌλ‘œ 이벀트λ₯Ό λ°œμƒμ‹œν‚¬ 수 있으며 트리 탐색 κΈ°λŠ₯μ΄λ‚˜ λ…Έλ“œ 정보 제곡 κΈ°λŠ₯을 κ°–λŠ”λ‹€.
λ…Έλ“œ κ°μ²΄λŠ” κ³΅ν†΅λœ κΈ°λŠ₯일수둝 ν”„λ‘œν† νƒ€μž… 체인의 μƒμœ„μ—, κ°œλ³„μ μΈ 고유 κΈ°λŠ₯일수둝 ν•˜μœ„μ— ν”„λ‘œν† νƒ€μž… 체인을 κ΅¬μΆ•ν•˜μ—¬ ν”„λ‘œνΌν‹°μ™€ λ©”μ„œλ“œλ₯Ό μ œκ³΅ν•˜λŠ” 상속 ꡬ쑰λ₯Ό κ°–λŠ”λ‹€.

### πŸ“ 39.2: μš”μ†Œ λ…Έλ“œ 취득

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

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

`document.getElementById` λ©”μ„œλ“œλŠ” 인수둜 μ „λ‹¬λœ `id` 값을 κ°–λŠ” 첫번째 μš”μ†Œ λ…Έλ“œλ§Œ λ°˜ν™˜ν•œλ‹€.
λ§Œμ•½ 인수둜 μ „λ‹¬λœ `id` 값을 κ°–λŠ” HTML μš”μ†Œκ°€ μ—†λŠ” 경우 `null`을 λ°˜ν™˜ν•œλ‹€.
HTML μš”μ†Œμ— `id` μ–΄νŠΈλ¦¬λ·°νŠΈλ₯Ό λΆ€μ—¬ν•˜λ©΄ `id` κ°’κ³Ό λ™μΌν•œ μ΄λ¦„μ˜ μ „μ—­ λ³€μˆ˜κ°€ μ•”λ¬΅μ μœΌλ‘œ μ„ μ–Έλ˜κ³  ν•΄λ‹Ή λ…Έλ“œ 객체가 ν• λ‹Ήλ˜λŠ” λΆ€μˆ˜ νš¨κ³Όκ°€ μžˆλ‹€.

```html
<!DOCTYPE html>
<html>
<body>
<div id="foo"></div>
<script>
console.log(foo === document.getElementById('foo')); // true
// 암묡적 μ „μ—­μœΌλ‘œ μƒμ„±λœ μ „μ—­ ν”„λ‘œνΌν‹°λŠ” μ‚­μ œλ˜μ§€λ§Œ μ „μ—­ λ³€μˆ˜λŠ” μ‚­μ œλ˜μ§€ μ•ŠμŒ
delete foo;
console.log(foo); // <div id="foo"></div>
</script>
</body>
</html>
```

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

`getElementsByTagName` λ©”μ„œλ“œλŠ” 인수둜 μ „λ‹¬ν•œ νƒœκ·Έ 이름을 κ°–λŠ” λͺ¨λ“  μš”μ†Œ λ…Έλ“œλ“€μ„ DOM μ»¬λ ‰μ…˜ 객체인 HTMLCollection 객체둜 λ°˜ν™˜ν•œλ‹€.
`getElementsByTagName` λ©”μ„œλ“œλŠ” `Document.prototype`에 μ •μ˜λœ λ©”μ„œλ“œμ™€ `Element.prototype`에 μ •μ˜λœ λ©”μ„œλ“œκ°€ μžˆλ‹€.
μ „μžλŠ” DOM의 루트 λ…Έλ“œμΈ λ¬Έμ„œ λ…Έλ“œ, 즉 `document`λ₯Ό 톡해 ν˜ΈμΆœν•˜λ©° μ „μ²΄μ—μ„œ μš”μ†Œ λ…Έλ“œλ₯Ό 탐색해 λ°˜ν™˜ν•œλ‹€.
ν•˜μ§€λ§Œ ν›„μžλŠ” νŠΉμ • μš”μ†Œ λ…Έλ“œλ₯Ό 톡해 ν˜ΈμΆœν•˜λ©°, νŠΉμ • μš”μ†Œ λ…Έλ“œμ˜ μžμ† λ…Έλ“œ μ€‘μ—μ„œ μš”μ†Œ λ…Έλ“œλ₯Ό 탐색해 λ°˜ν™˜ν•œλ‹€.
인수둜 μ „λ‹¬λœ νƒœκ·Έ 이름을 κ°–λŠ” μš”μ†Œκ°€ μ—†λ‹€λ©΄ 빈 HTMLCollection 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.

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

`getElementsByClassName` λ©”μ„œλ“œλŠ” 인수둜 μ „λ‹¬ν•œ `class` 값을 κ°–λŠ” λͺ¨λ“  μš”μ†Œ λ…Έλ“œλ“€μ„ 탐색해 λ°˜ν™˜ν•œλ‹€.
`getElementsByTagName` λ©”μ„œλ“œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ HTMLCollection 객체λ₯Ό λ°˜ν™˜ν•˜λ©°, `Document.prototype`와 `Element.prototype`에 μ •μ˜λœ λ©”μ„œλ“œκ°€ μžˆλ‹€.
인수둜 μ „λ‹¬λœ `class` 값을 κ°–λŠ” μš”μ†Œκ°€ μ—†λ‹€λ©΄ 빈 HTMLCollection 객체λ₯Ό λ°˜ν™˜ν•œλ‹€.

0 comments on commit 067b9a0

Please sign in to comment.