Skip to content

Commit 27ee1de

Browse files
committed
Translate: Dom Manipulation(~ Document Interface)
* ~ Basic Example λ²ˆμ—­ * ~ The Document Interface λ²ˆμ—­ * λ§žμΆ€λ²•κ²€μ‚¬, ν…ŒμŠ€νŠΈ
1 parent 091e3e0 commit 27ee1de

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

β€Žpages/tutorials/dom-manipulation.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ permalink: /docs/handbook/dom-manipulation.html
55
oneline: Using the DOM with TypeScript
66
---
77

8-
# DOM Manipulation
8+
# DOM μ‘°μž‘ (DOM Manipulation)
99

10-
### _An exploration into the `HTMLElement` type_
10+
### _`HTMLElement` νƒ€μž… 탐ꡬ_ (_An exploration into the `HTMLElement` type_)
1111

12-
In the 20+ years since its standardization, JavaScript has come a very long way. While in 2020, JavaScript can be used on servers, in data science, and even on IoT devices, it is important to remember its most popular use case: web browsers.
12+
ν‘œμ€€ν™” 이후 20μ—¬ λ…„ λ™μ•ˆ, JavaScriptλŠ” λ§Žμ€ λ°œμ „μ„ μ΄λ£¨μ—ˆμŠ΅λ‹ˆλ‹€. 2020λ…„μ—λŠ” μ„œλ²„, 데이터 μ‚¬μ΄μ–ΈμŠ€, 그리고 IoT 기기에도 JavaScriptλ₯Ό μ‚¬μš©ν•  수 μžˆμ§€λ§Œ, κ°€μž₯ 인기 μžˆλŠ” ν™œμš© μ‚¬λ‘€λŠ” μ›Ή λΈŒλΌμš°μ €μΈ 것을 κΈ°μ–΅ν•˜λŠ” 것이 μ€‘μš”ν•©λ‹ˆλ‹€.
1313

14-
Website are made up of HTML and/or XML documents. These documents are static, they do not change. The D*ocument Object Model (DOM) is* a programming interface implemented by browsers in order to make static websites functional. The DOM API can be used to change the document structure, style, and content. The API is so powerful that countless frontend frameworks (jQuery, React, Angular, etc.) have been developed around it in order to make dynamic websites even easier to develop.
14+
μ›Ή μ‚¬μ΄νŠΈλŠ” HTML 및/λ˜λŠ” XML λ¬Έμ„œλ‘œ κ΅¬μ„±λ©λ‹ˆλ‹€. μ΄λŸ¬ν•œ λ¬Έμ„œλ“€μ€ μ •μ μ΄μ–΄μ„œ, λ³€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. *λ¬Έμ„œ 객체 λͺ¨λΈ(DOM)은* 정적 μ›Ή μ‚¬μ΄νŠΈλ₯Ό κΈ°λŠ₯적으둜 μž‘λ™μ‹œν‚€κΈ° μœ„ν•΄ λΈŒλΌμš°μ €μ— μ˜ν•΄ κ΅¬ν˜„λœ ν”„λ‘œκ·Έλž˜λ° μΈν„°νŽ˜μ΄μŠ€μž…λ‹ˆλ‹€. DOM APIλ₯Ό μ‚¬μš©ν•˜λ©΄ λ¬Έμ„œμ˜ ꡬ쑰, μŠ€νƒ€μΌ, 그리고 λ‚΄μš©μ„ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€. APIλŠ” 맀우 κ°•λ ₯ν•΄μ„œ 이λ₯Ό λ°”νƒ•μœΌλ‘œ 보닀 μ‰½κ²Œ 동적인 μ›Ήμ‚¬μ΄νŠΈλ“€ κ°œλ°œν•˜κΈ° μœ„ν•΄ μˆ˜λ§Žμ€ ν”„λŸ°νŠΈμ—”λ“œ ν”„λ ˆμž„μ›Œν¬(jQuery, React, Angular λ“±)κ°€ κ°œλ°œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.
1515

16-
TypeScript is a typed superset of JavaScript, and it ships type definitions for the DOM API. These definitions are readily available in any default TypeScript project. Of the 20,000+ lines of definitions in _lib.dom.d.ts_, one stands out among the rest: `HTMLElement` . This type is the backbone for DOM manipulation with TypeScript.
16+
TypeScriptλŠ” νƒ€μž…μ΄ μžˆλŠ” JavaScript μƒμœ„ μ§‘ν•©(superset)이며, DOM API에 λŒ€ν•œ νƒ€μž… μ •μ˜λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€. μ΄λŸ¬ν•œ μ •μ˜λŠ” λͺ¨λ“  κΈ°λ³Έ TypeScript ν”„λ‘œμ νŠΈμ—μ„œ μ‰½κ²Œ μ‚¬μš© κ°€λŠ₯ν•©λ‹ˆλ‹€. _lib.dom.d.ts_ 에 μžˆλŠ” 2λ§Œμ—¬ μ€„μ˜ μ •μ˜ μ€‘μ—μ„œ, κ°€μž₯ λˆˆμ— λ„λŠ” 것은 `HTMLElement`μž…λ‹ˆλ‹€. 이 νƒ€μž…μ€ TypeScriptλ₯Ό μ‚¬μš©ν•œ DOM μ‘°μž‘μ˜ μ€‘μΆ•μž…λ‹ˆλ‹€.
1717

18-
> You can explore the source code for the [DOM type definitions](https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts)
18+
> [DOM νƒ€μž… μ •μ˜](https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts)에 λŒ€ν•œ μ†ŒμŠ€μ½”λ“œλŠ” μ΄κ³³μ—μ„œ λ³Ό 수 μžˆμŠ΅λ‹ˆλ‹€.
1919
20-
## Basic Example
20+
## 기본 예제 (Basic Example)
2121

22-
Given a simplified _index.html_ file:
22+
κ°„λ‹¨ν•œ μ˜ˆμ‹œ 파일 _index.html_:
2323

2424
<!DOCTYPE html>
2525
<html lang="en">
@@ -31,60 +31,60 @@ Given a simplified _index.html_ file:
3131
</body>
3232
</html>
3333

34-
Lets explore a TypeScript script that adds a `<p>Hello, World</p>` element to the `#app` element.
34+
`#app` μš”μ†Œμ— `<p>Hello, World</p>` μš”μ†Œλ₯Ό μΆ”κ°€ν•˜λŠ” TypeScript 슀크립트λ₯Ό μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€.
3535

3636
```ts
37-
// 1. Select the div element using the id property
37+
// 1. id ν”„λ‘œνΌν‹°λ₯Ό μ‚¬μš©ν•˜μ—¬ div μš”μ†Œλ₯Ό μ„ νƒν•©λ‹ˆλ‹€.
3838
const app = document.getElementById("app");
3939

40-
// 2. Create a new <p></p> element programmatically
40+
// 2. μƒˆλ‘œμš΄ <p></p> μš”μ†Œλ₯Ό ν”„λ‘œκ·Έλž˜λ° λ°©μ‹μœΌλ‘œ μƒμ„±ν•©λ‹ˆλ‹€.
4141
const p = document.createElement("p");
4242

43-
// 3. Add the text content
43+
// 3. ν…μŠ€νŠΈ λ‚΄μš©μ„ μΆ”κ°€ν•©λ‹ˆλ‹€.
4444
p.textContent = "Hello, World!";
4545

46-
// 4. Append the p element to the div element
46+
// 4. div μš”μ†Œμ— p μš”μ†Œλ₯Ό μžμ‹ λ…Έλ“œλ‘œ μΆ”κ°€ν•©λ‹ˆλ‹€.
4747
app?.appendChild(p);
4848
```
4949

50-
After compiling and running the _index.html_ page, the resulting HTML will be:
50+
_index.html_ νŽ˜μ΄μ§€λ₯Ό μ»΄νŒŒμΌν•˜κ³  μ‹€ν–‰ν•œ ν›„, HTML κ²°κ³Ό:
5151

5252
```html
5353
<div id="app">
5454
<p>Hello, World!</p>
5555
</div>
5656
```
5757

58-
## The `Document` Interface
58+
## `Document` μΈν„°νŽ˜μ΄μŠ€ (The `Document` Interface)
5959

60-
The first line of the TypeScript code uses a global variable `document` , inspecting the variable shows it is defined by the `Document` interface from the _lib.dom.d.ts_ file. The code snippet contains calls to two methods, `getElementById` and `createElement`.
60+
TypeScript μ½”λ“œμ˜ 첫 번째 쀄은 μ „μ—­λ³€μˆ˜ `document`λ₯Ό μ‚¬μš©ν•˜λ©°, κ·Έ λ³€μˆ˜λ₯Ό κ²€μ‚¬ν•˜λ©΄ _lib.dom.d.ts_ 파일의 `Document` μΈν„°νŽ˜μ΄μŠ€μ— μ˜ν•΄ μ •μ˜λœ κ²ƒμœΌλ‘œ ν‘œμ‹œλ©λ‹ˆλ‹€. κ·Έ μ½”λ“œμ˜ μŠ€λ‹ˆνŽ«(snippet)μ—λŠ” `getElementById`와 `createElement`λΌλŠ” 두 κ°€μ§€ λ©”μ„œλ“œ 호좜이 ν¬ν•¨λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.
6161

6262
### `Document.getElementById`
6363

64-
The definition for this method is as follows:
64+
이 λ©”μ„œλ“œμ˜ μ •μ˜λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€:
6565

6666
```ts
6767
getElementById(elementId: string): HTMLElement | null;
6868
```
6969

70-
Pass it an element id string and it will return either `HTMLElement` or `null` . This method introduces one of the most important types, `HTMLElement`. It serves as the base interface for every other element interface. For example, the `p` variable in the code example is of type `HTMLParagraphElement`. Also take note that this method can return `null`. This is because the method can't be certain pre-runtime if it will be able to actually find the specified element or not. In the last line of the code snippet, the new _optional chaining_ operator is used in order to call `appendChild`.
70+
λ¬Έμžμ—΄ id μš”μ†Œκ°€ μ „λ‹¬λ˜λ©΄ `HTMLElement` λ˜λŠ” `null`이 λ°˜ν™˜λ©λ‹ˆλ‹€. 이 λ©”μ„œλ“œλŠ” κ°€μž₯ μ€‘μš”ν•œ νƒ€μž…λ“€ 쀑 ν•˜λ‚˜μΈ `HTMLElement`λ₯Ό λ„μž…ν•©λ‹ˆλ‹€. 이 νƒ€μž…μ€ λ‹€λ₯Έ λͺ¨λ“  μš”μ†Œ μΈν„°νŽ˜μ΄μŠ€μ˜ κΈ°λ³Έ μΈν„°νŽ˜μ΄μŠ€ 역할을 ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€λ©΄, 예제 μ½”λ“œμ—μ„œ `p` λ³€μˆ˜λŠ” `HTMLParagraphElement` νƒ€μž…μž…λ‹ˆλ‹€. λ‹€μŒμœΌλ‘œ, 이 λ©”μ„œλ“œλŠ” `null`을 λ°˜ν™˜ν•  수 μžˆλ‹€λŠ” 점에 μœ μ˜ν•΄μ•Ό ν•©λ‹ˆλ‹€. λ©”μ„œλ“œκ°€ μ‹€μ œλ‘œ μ§€μ •λœ μš”μ†Œλ₯Ό 찾을 수 μžˆμ„μ§€ 없을지에 따라 ν™•μ‹€ν•œ 사전 λŸ°νƒ€μž„μ΄ 될 수 μ—†κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. μŠ€λ‹ˆνŽ« μ½”λ“œμ˜ λ§ˆμ§€λ§‰ μ€„μ—λŠ”, `appendChild`λ₯Ό ν˜ΈμΆœν•˜κΈ° μœ„ν•΄ μƒˆλ‘œμš΄ _선택적 체이닝(optional chaining)_ μ—°μ‚°μžκ°€ μ‚¬μš©λ˜κ³  μžˆμŠ΅λ‹ˆλ‹€.
7171

7272
### `Document.createElement`
7373

74-
The definition for this method is (I have omitted the _deprecated_ definition):
74+
이 λ©”μ„œλ“œμ˜ μ •μ˜λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€(_deprecated_ ν‘œκΈ°λœ μ •μ˜λŠ” μƒλž΅ν–ˆμŠ΅λ‹ˆλ‹€):
7575

7676
```ts
7777
createElement<K extends keyof HTMLElementTagNameMap>(tagName: K, options?: ElementCreationOptions): HTMLElementTagNameMap[K];
7878
createElement(tagName: string, options?: ElementCreationOptions): HTMLElement;
7979
```
8080
81-
This is an overloaded function definition. The second overload is simplest and works a lot like the `getElementById` method does. Pass it any `string` and it will return a standard HTMLElement. This definition is what enables developers to create unique HTML element tags.
81+
μ΄λŠ” μ˜€λ²„ λ‘œλ“œλœ ν•¨μˆ˜ μ •μ˜μž…λ‹ˆλ‹€. 두 번째 μ˜€λ²„λ‘œλ“œλŠ” κ°€μž₯ λ‹¨μˆœν•˜λ©° `getElementById` λ©”μ„œλ“œμ™€ 맀우 μœ μ‚¬ν•˜κ²Œ μž‘λ™ν•©λ‹ˆλ‹€. μ–΄λ–€ `λ¬Έμžμ—΄`이 μ „λ‹¬λ˜λ©΄ HTMLElement ν‘œμ€€μ΄ λ°˜ν™˜λ©λ‹ˆλ‹€. 이 μ •μ˜λŠ” κ°œλ°œμžκ°€ κ³ μœ ν•œ HTML μš”μ†Œ νƒœκ·Έλ₯Ό 생성할 수 있게 ν•©λ‹ˆλ‹€.
8282
83-
For example `document.createElement('xyz')` returns a `<xyz></xyz>` element, clearly not an element that is specified by the HTML specification.
83+
예λ₯Ό λ“€λ©΄ `document.createElement('xyz')`λŠ” HTML κ·œκ²©μ— μ§€μ •λœ μš”μ†Œκ°€ μ•„λ‹Œ `<xyz></xyz>` μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
8484
85-
> For those interested, you can interact with custom tag elements using the `document.getElementsByTagName`
85+
> 관심 μžˆλŠ” μ‚¬λžŒλ“€μ„ μœ„ν•΄ μ–ΈκΈ‰ν•˜μžλ©΄, `document.getElementsByTagName`λ₯Ό μ‚¬μš©ν•΄ μ‚¬μš©μž μ •μ˜ νƒœκ·Έ(custom tag) μš”μ†Œμ™€ μƒν˜Έμž‘μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
8686
87-
For the first definition of `createElement`, it is using some advanced generic patterns. It is best understood broken down into chunks. Starting with the generic expression: `<K extends keyof HTMLElementTagNameMap>`. This expression defines a generic parameter `K` that is _constrained_ to the keys of the interface `HTMLElementTagNameMap`. The map interface contains every specified HTML tag name and its corresponding type interface. For example here are the first 5 mapped values:
87+
`createElement`의 첫 번째 μ •μ˜μ—λŠ”, κ³ κΈ‰ μ œλ„€λ¦­ νŒ¨ν„΄λ“€μ„ μ‚¬μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” λ‚΄μš©μ„ λ‚˜λˆ„μ–΄ μ΄ν•΄ν•˜λŠ” 것이 κ°€μž₯ μ’‹μŠ΅λ‹ˆλ‹€. λ¨Όμ € `<K extends keyof HTMLElementTagNameMap>`λΌλŠ” μ œλ„€λ¦­ ν‘œν˜„λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€. 이 ν‘œν˜„μ‹μ€ `HTMLElementTagNameMap` μΈν„°νŽ˜μ΄μŠ€μ˜ 킀에 μ œμ•½μ„ λ°›λŠ” μ œλ„€λ¦­ λ§€κ°œλ³€μˆ˜ `K`λ₯Ό μ •μ˜ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. κ·Έ λ§΅ μΈν„°νŽ˜μ΄μŠ€λŠ” λͺ¨λ“  μ§€μ •λœ HTML νƒœκ·Έ 이름과 ν•΄λ‹Ή νƒ€μž… μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν¬ν•¨ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. 예λ₯Ό λ“€λ©΄ μ—¬κΈ° μ½”λ“œ μ΄ˆλ°˜μ— 5개의 λ§€ν•‘λœ 값이 μžˆμŠ΅λ‹ˆλ‹€:
8888
8989
```ts
9090
interface HTMLElementTagNameMap {
@@ -97,9 +97,9 @@ interface HTMLElementTagNameMap {
9797
}
9898
```
9999

100-
Some elements do not exhibit unique properties and so they just return `HTMLElement`, but other types do have unique properties and methods so they return their specific interface (which will extend from or implement `HTMLElement`).
100+
일뢀 μš”μ†Œλ“€μ€ κ³ μœ ν•œ ν”„λ‘œνΌν‹°λ₯Ό λ‚˜νƒ€λ‚΄μ§€ μ•Šμ•„ `HTMLElement`λ₯Ό λ°˜ν™˜ν•˜κΈ°λ„ ν•˜μ§€λ§Œ, κ·Έ μ™Έ νƒ€μž… μš”μ†Œλ“€μ€ κ³ μœ ν•œ ν”„λ‘œνΌν‹°μ™€ λ©”μ„œλ“œλ₯Ό κ°€μ§€κ³  νŠΉμ • μΈν„°νŽ˜μ΄μŠ€(`HTMLElement`μ—μ„œ ν™•μž₯λ˜κ±°λ‚˜ κ΅¬ν˜„λ¨)λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
101101

102-
Now, for the remainder of the `createElement` definition: `(tagName: K, options?: ElementCreationOptions): HTMLElementTagNameMap[K]`. The first argument `tagName` is defined as the generic parameter `K` . The TypeScript interpreter is smart enough to _infer_ the generic parameter from this argument. This means that the developer does not actually have to specify the generic parameter when using the method; whatever value is passed to the `tagName` argument will be inferred as `K` and thus can be used throughout the remainder of the definition. Which is exactly what happens; the return value `HTMLElementTagNameMap[K]` takes the `tagName` argument and uses it to return the corresponding type. This definition is how the `p` variable from the code snippet gets a type of `HTMLParagraphElement`. And if the code was `document.createElement('a')`, then it would be a element of type `HTMLAnchorElement`.
102+
이제, `createElement` μ •μ˜μ˜ λ‚˜λ¨Έμ§€ 뢀뢄인 `(tagName: K, options?: ElementCreationOptions): HTMLElementTagNameMap[K]`λ₯Ό μ‚΄νŽ΄λ³΄κ² μŠ΅λ‹ˆλ‹€. 첫 번째 인수 `tagName`은 일반적 λ§€κ°œλ³€μˆ˜ `K`둜 μ •μ˜λ©λ‹ˆλ‹€. TypeScript μΈν„°ν”„λ¦¬ν„°λŠ” 이 μΈμˆ˜λ‘œλΆ€ν„° 일반 λ§€κ°œλ³€μˆ˜λ₯Ό _μΆ”λ‘ _ ν•  수 μžˆλŠ” μΆ©λΆ„ν•œ μ„±λŠ₯을 κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€. μ΄λŠ” κ°œλ°œμžκ°€ λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  λ•Œ μ‹€μ œλ‘œ 일반적인 λ§€κ°œλ³€μˆ˜λ₯Ό μ§€μ •ν•  ν•„μš”κ°€ μ—†μŒμ„ μ˜λ―Έν•˜λ©°, μ–΄λ–€ 값이 `tagName`인수둜 μ „λ‹¬λ˜λ“  간에 `K`둜 μΆ”λ‘ λ˜λ―€λ‘œ μ •μ˜μ˜ λ‚˜λ¨Έμ§€ 뢀뢄에 μ‚¬μš©ν•  수 μžˆμ„ κ²ƒμž…λ‹ˆλ‹€. μ •ν™•νžˆ 무슨 일이 μΌμ–΄λ‚˜λŠ”μ§€λ₯Ό 보면 λ°˜ν™˜κ°’ `HTMLElementTagNameMap[K]`λŠ” `tagName`인수λ₯Ό κ°€μ§€κ³  ν•΄λ‹Ή νƒ€μž…μ„ λ°˜ν™˜ν•©λ‹ˆλ‹€. 이 μ •μ˜λŠ” μŠ€λ‹ˆνŽ« μ½”λ“œ `p` λ³€μˆ˜μ—μ„œ `HTMLParagraphElement`νƒ€μž…μ„ μ–»λŠ” λ°©λ²•μž…λ‹ˆλ‹€. 그리고 μ½”λ“œκ°€ `document.createElement('a')`μ˜€λ‹€λ©΄, `HTMLAnchorElement`νƒ€μž…μ˜ μš”μ†Œκ°€ λ©λ‹ˆλ‹€.
103103

104104
## The `Node` interface
105105

0 commit comments

Comments
Β (0)