Skip to content

Commit 5ee946f

Browse files
[번역] focus와 blur
1 parent 4d484da commit 5ee946f

File tree

1 file changed

+74
-75
lines changed

1 file changed

+74
-75
lines changed
Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,62 @@
1-
# Focusing: focus/blur
1+
# focus와 blur
22

3-
An element receives the focus when the user either clicks on it or uses the `key:Tab` key on the keyboard. There's also an `autofocus` HTML attribute that puts the focus onto an element by default when a page loads and other means of getting the focus.
3+
사용자가 폼 요소를 클릭하거나 `key:Tab` 키를 눌러 요소로 이동하면 해당 요소가 포커스(focus)됩니다. `autofocus`라는 HTML 속성을 사용해도 요소를 포커스 할 수 있는데 이 속성이 있는 요소는 페이지가 로드된 후 자동으로 포커싱 됩니다. 이 외에도 요소를 포커싱(focusing)할 수 있는 방법은 다양합니다.
44

5-
Focusing on an element generally means: "prepare to accept the data here", so that's the moment when we can run the code to initialize the required functionality.
5+
요소를 포커싱한다는 것은 일반적으로 '여기에 데이터를 입력할 준비를 하라'는 것을 의미하기 때문에 요소 포커싱이 이뤄지는 순간엔 요구사항을 충족시키는 초기화 코드를 실행할 수 있습니다.
66

7-
The moment of losing the focus ("blur") can be even more important. That's when a user clicks somewhere else or presses `key:Tab` to go to the next form field, or there are other means as well.
7+
요소가 포커스를 잃는 순간(blur)은 요소가 포커스를 얻는 순간보다 더 중요할 수 있습니다. 사용자가 다른 곳을 클릭하거나 `key:Tab` 키를 눌러 다음 폼 필드로 이동하면 포커스 상태의 요소가 포커스를 잃게 됩니다. 이 외에도 다양한 방법을 사용해 포커스를 잃게 할 수 있습니다.
88

9-
Losing the focus generally means: "the data has been entered", so we can run the code to check it or even to save it to the server and so on.
9+
요소가 포커스를 잃는 것은 대개 '데이터 입력이 완료되었다'는 것을 의미하기 때문에 포커싱이 해제되는 순간엔 데이터를 체크하거나 입력된 데이터를 저장하기 위해 서버에 요청을 보내는 등의 코드를 실행할 수 있습니다.
1010

11-
There are important peculiarities when working with focus events. We'll do the best to cover them further on.
11+
포커스 이벤트를 다룰 땐 이상해 보이지만 중요한 기능이 있는데, 이번 챕터에선 이에 대해서 자세히 다뤄보도록 하겠습니다.
1212

13-
## Events focus/blur
13+
## focus, blur 이벤트
1414

15-
The `focus` event is called on focusing, and `blur` -- when the element loses the focus.
15+
`focus` 이벤트는 요소가 포커스를 받을 때, `blur` 이벤트는 포커스를 잃을 때 발생합니다.
1616

17-
Let's use them for validation of an input field.
17+
두 이벤트를 입력 필드 값 검증에 사용해 봅시다.
1818

19-
In the example below:
19+
예시에서 각 핸들러는 다음과 같은 역할을 합니다.
2020

21-
- The `blur` handler checks if the field has an email entered, and if not -- shows an error.
22-
- The `focus` handler hides the error message (on `blur` it will be checked again):
21+
- `blur` 핸들러에선 필드에 이메일이 잘 입력되었는지 확인하고 잘 입력되지 않은 경우엔 에러를 보여줍니다.
22+
- `focus` 핸들러에선 에러 메시지를 숨깁니다(이메일 재확인은 `blur` 핸들러에서 합니다).
2323

2424
```html run autorun height=60
2525
<style>
2626
.invalid { border-color: red; }
2727
#error { color: red }
2828
</style>
2929

30-
Your email please: <input type="email" id="input">
30+
이메일: <input type="email" id="input">
3131

3232
<div id="error"></div>
3333

3434
<script>
3535
*!*input.onblur*/!* = function() {
36-
if (!input.value.includes('@')) { // not email
36+
if (!input.value.includes('@')) { // @ 유무를 이용해 유효한 이메일 주소가 아닌지 체크
3737
input.classList.add('invalid');
38-
error.innerHTML = 'Please enter a correct email.'
38+
error.innerHTML = '올바른 이메일 주소를 입력하세요.'
3939
}
4040
};
4141
4242
*!*input.onfocus*/!* = function() {
4343
if (this.classList.contains('invalid')) {
44-
// remove the "error" indication, because the user wants to re-enter something
44+
// 사용자가 새로운 값을 입력하려고 하므로 에러 메시지를 지움
4545
this.classList.remove('invalid');
4646
error.innerHTML = "";
4747
}
4848
};
4949
</script>
5050
```
5151

52-
Modern HTML allows us to do many validations using input attributes: `required`, `pattern` and so on. And sometimes they are just what we need. JavaScript can be used when we want more flexibility. Also we could automatically send the changed value to the server if it's correct.
52+
모던 HTML을 사용하면 `required`, `pattern` 등의 다양한 속성을 사용해 입력값을 검증 할 수 있습니다. HTML 속성만으로도 검증이 가능한거죠. 그럼에도 불구하고 자바스크립트를 사용하는 이유는 자바스크립트가 좀 더 유연하기 때문입니다. 여기에 더하여 자바스크립트를 사용하면 제대로 된 값이 입력되었을 때 자동으로 해당값을 서버에 보낼 수 있기 때문이기도 합니다.
5353

5454

55-
## Methods focus/blur
55+
## focus, blur 메서드
5656

57-
Methods `elem.focus()` and `elem.blur()` set/unset the focus on the element.
57+
`elem.focus()``elem.blur()` 메서드를 사용하면 요소에 포커스를 줄 수도 있고 제거할 수도 있습니다.
5858

59-
For instance, let's make the visitor unable to leave the input if the value is invalid:
59+
사이트 방문자가 유효하지 않은 값을 입력하면 사이트를 떠나지 못하도록 하는 예시를 살펴봅시다.
6060

6161
```html run autorun height=80
6262
<style>
@@ -65,16 +65,16 @@ For instance, let's make the visitor unable to leave the input if the value is i
6565
}
6666
</style>
6767

68-
Your email please: <input type="email" id="input">
69-
<input type="text" style="width:220px" placeholder="make email invalid and try to focus here">
68+
이메일: <input type="email" id="input">
69+
<input type="text" style="width:220px" placeholder="이메일 형식이 아닌 값을 입력하고 여기에 포커스를 주세요.">
7070

7171
<script>
7272
input.onblur = function() {
73-
if (!this.value.includes('@')) { // not email
74-
// show the error
73+
if (!this.value.includes('@')) { // 이메일이 아님
74+
// 에러 출력
7575
this.classList.add("error");
7676
*!*
77-
// ...and put the focus back
77+
// input 필드가 포커스 되도록 함
7878
input.focus();
7979
*/!*
8080
} else {
@@ -84,11 +84,11 @@ Your email please: <input type="email" id="input">
8484
</script>
8585
```
8686

87-
It works in all browsers except Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=53579)).
87+
이 예시는 Firefox([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=53579))를 제외한 모든 브라우저에서 정상 동작합니다.
8888

89-
If we enter something into the input and then try to use `key:Tab` or click away from the `<input>`, then `onblur` returns the focus back.
89+
이메일이 아닌 값을 입력하고 `key:Tab` 키나 다른 곳을 클릭해 `<input>`을 벗어나려 하면 `onblur` 메서드가 동작해 포커스를 다시 입력 필드로 돌려놓습니다.
9090

91-
Please note that we can't "prevent losing focus" by calling `event.preventDefault()` in `onblur`, because `onblur` works *after* the element lost the focus.
91+
여기서 주의해야 할 점은 `onblur`는 요소가 포커스를 잃고 난 **에 발생하기 때문에 `onblur` 안에서 `event.preventDefault()`를 호출해 포커스를 잃게 하는걸 '막을 수 없다'라는 사실입니다.
9292

9393
```warn header="JavaScript-initiated focus loss"
9494
A focus loss can occur for many reasons.
@@ -102,41 +102,41 @@ These features sometimes cause `focus/blur` handlers to misbehave -- to trigger
102102
103103
The best recipe is to be careful when using these events. If we want to track user-initiated focus-loss, then we should avoid causing it ourselves.
104104
```
105-
## Allow focusing on any element: tabindex
105+
## tabindex를 사용해서 모든 요소 포커스 하기
106106

107-
By default many elements do not support focusing.
107+
대다수의 요소는 기본적으로 포커싱을 지원하지 않습니다.
108108

109-
The list varies a bit between browsers, but one thing is always correct: `focus/blur` support is guaranteed for elements that a visitor can interact with: `<button>`, `<input>`, `<select>`, `<a>` and so on.
109+
포커싱을 지원하지 않는 요소 목록은 브라우저마다 다르긴 하지만 한 가지 확실한 것은 `<button>`, `<input>`, `<select>`, `<a>`와 같이 사용자가 웹 페이지와 상호작용 할 수 있게 도와주는 요소는 `focus`, `blur`를 지원한다는 사실입니다.
110110

111-
On the other hand, elements that exist to format something, such as `<div>`, `<span>`, `<table>` -- are unfocusable by default. The method `elem.focus()` doesn't work on them, and `focus/blur` events are never triggered.
111+
반면 `<div>`, `<span>`, `<table>`같이 무언가를 표시하는 용도로 사용하는 요소들은 포커싱을 지원하지 않습니다. 따라서 이런 요소엔 `elem.focus()` 메서드가 동작하지 않고 `focus`, `blur`이벤트도 트리거 되지 않습니다.
112112

113-
This can be changed using HTML-attribute `tabindex`.
113+
그럼에도 불구하고 포커스를 하고 싶다면 `tabindex` HTML 속성을 사용하면 됩니다.
114114

115-
Any element becomes focusable if it has `tabindex`. The value of the attribute is the order number of the element when `key:Tab` (or something like that) is used to switch between them.
115+
`tabindex` 속성이 있는 요소는 종류와 상관없이 포커스가 가능합니다. 속성값은 숫자인데, 이 숫자는 `key:Tab` 키를 눌러 요소 사이를 이동할 때 순서가 됩니다.
116116

117-
That is: if we have two elements, the first has `tabindex="1"`, and the second has `tabindex="2"`, then pressing `key:Tab` while in the first element -- moves the focus into the second one.
117+
요소가 두 개 있다고 가정하고 첫 번째 요소의 `tabindex``1`을, 두 번째 요소의 `tabindex``2`를 할당하면 첫 번째 요소가 포커싱되어있는 상태에서 `key:Tab`을 눌렀을 때 두 번째 요소로 포커스가 이동합니다.
118118

119-
The switch order is: elements with `tabindex` from `1` and above go first (in the `tabindex` order), and then elements without `tabindex` (e.g. a regular `<input>`).
119+
포커싱 되는 요소 순서는 다음과 같습니다. `tabindex``1`인 요소부터 시작해 점점 큰 숫자가 매겨진 요소로 이동하고 그다음 `tabindex`가 없는 요소(평범한 `<input>` 요소 등)로 이동합니다.
120120

121-
Elements with matching `tabindex` are switched in the document source order (the default order).
121+
`tabindex`가 없는 요소들은 문서 내 순서에 따라 포커스가 이동합니다(기본 순서).
122122

123-
There are two special values:
123+
그런데 `tabindex`를 사용할 땐 주의해야 할 사항이 있습니다.
124124

125-
- `tabindex="0"` puts an element among those without `tabindex`. That is, when we switch elements, elements with `tabindex=0` go after elements with `tabindex ≥ 1`.
125+
- `tabindex``0`인 요소 -- 이 요소는 `tabindex` 속성이 없는것처럼 동작합니다. 따라서 포커스를 이동시킬 때 `tabindex``0`인 요소는 `tabindex`가 1보다 크거나 같은 요소보다 나중에 포커스를 받습니다.
126126

127-
Usually it's used to make an element focusable, but keep the default switching order. To make an element a part of the form on par with `<input>`.
127+
`tabindex="0"`은 요소를 포커스 가능하게 만들지만 포커스 순서는 기본 순서 그대로 유지하고 싶을 때 사용합니다. 요소의 포커스 우선 순위를 일반 `<input>`과 같아지도록 하죠.
128128

129-
- `tabindex="-1"` allows only programmatic focusing on an element. The `key:Tab` key ignores such elements, but method `elem.focus()` works.
129+
- `tabindex``-1`인 요소 -- 스크립트로만 포커스 하고 싶은 요소에 사용합니다. `key:Tab`키를 사용하면 이 요소는 무시되지만 `elem.focus()` 메서드를 사용하면 잘 포커싱 됩니다.
130130

131-
For instance, here's a list. Click the first item and press `key:Tab`:
131+
예시를 살펴봅시다. 첫 번째 항목을 클릭하고 `key:Tab` 키를 눌러보세요.
132132

133133
```html autorun no-beautify
134-
Click the first item and press Tab. Keep track of the order. Please note that many subsequent Tabs can move the focus out of the iframe in the example.
134+
첫 번째 항목을 클릭하고 `key:Tab` 키를 눌러보면서 포커스 된 요소 순서를 눈여겨보세요. 참고로 탭을 많이 누르면 예시 밖으로 포커스가 이동하니, 주의하세요.
135135
<ul>
136-
<li tabindex="1">One</li>
137-
<li tabindex="0">Zero</li>
138-
<li tabindex="2">Two</li>
139-
<li tabindex="-1">Minus one</li>
136+
<li tabindex="1"></li>
137+
<li tabindex="0"></li>
138+
<li tabindex="2"></li>
139+
<li tabindex="-1">음수 일</li>
140140
</ul>
141141

142142
<style>
@@ -145,63 +145,62 @@ Click the first item and press Tab. Keep track of the order. Please note that ma
145145
</style>
146146
```
147147

148-
The order is like this: `1 - 2 - 0`. Normally, `<li>` does not support focusing, but `tabindex` full enables it, along with events and styling with `:focus`.
148+
보시다시피 포커스는 `tabindex``1`, `2`, `0`인 요소로 차례로 이동합니다. `<li>`는 기본적으로 포커스 할 수 없는 요소이지만 예시에서 `tabindex`를 사용해서 실제 포커스를 해보았고 포커스 된 요소는 `:focus`를 사용해서 스타일을 바꿔보았습니다.
149149

150-
```smart header="The property `elem.tabIndex` works too"
151-
We can add `tabindex` from JavaScript by using the `elem.tabIndex` property. That has the same effect.
150+
```smart header="`elem.tabIndex` 프로퍼티를 사용해도 됩니다."
151+
자바스크립트를 사용해 `elem.tabIndex` 프로퍼티를 추가해주면 `tabindex` 속성을 사용한 것과 동일한 효과를 볼 수 있습니다.
152152
```
153153
154-
## Delegation: focusin/focusout
154+
## focusin과 focusout을 사용해 이벤트 위임하기
155155
156-
Events `focus` and `blur` do not bubble.
156+
`focus``blur` 이벤트는 버블링 되지 않습니다.
157157
158-
For instance, we can't put `onfocus` on the `<form>` to highlight it, like this:
158+
예시를 살펴봅시다. `<form>`에 `onfocus`를 추가해 강조 효과를 주려고 했는데, 의도한 대로 동작하지 않는 것을 확인할 수 있습니다.
159159
160160
```html autorun height=80
161-
<!-- on focusing in the form -- add the class -->
161+
<!-- 폼 안에서 포커스가 된 경우 클래스를 추가함 -->
162162
<form *!*onfocus="this.className='focused'"*/!*>
163-
<input type="text" name="name" value="Name">
164-
<input type="text" name="surname" value="Surname">
163+
<input type="text" name="surname" value="">
164+
<input type="text" name="name" value="이름">
165165
</form>
166166
167167
<style> .focused { outline: 1px solid red; } </style>
168168
```
169169

170-
The example above doesn't work, because when user focuses on an `<input>`, the `focus` event triggers on that input only. It doesn't bubble up. So `form.onfocus` never triggers.
170+
의도한 대로 예시가 동작하지 않는 이유는 사용자가 `<input>`을 포커스 해도 `focus` 이벤트는 해당 입력 필드에서만 트리거 되기 때문입니다. `focus` 이벤트는 버블링 되지 않습니다. 따라서 `form.onfocus`는 절대 트리거 되지 않습니다.
171171

172-
There are two solutions.
172+
이런 기본동작을 피해 이벤트 위임 효과를 주는 방법은 두 가지가 있습니다.
173173

174-
First, there's a funny historical feature: `focus/blur` do not bubble up, but propagate down on the capturing phase.
174+
첫 번째 방법은 `focus``blur`는 버블링 되지 않지만 캡처링은 된다는 점을 이용하면 됩니다.
175175

176-
This will work:
176+
아래 예시를 직접 실행해봅시다.
177177

178178
```html autorun height=80
179179
<form id="form">
180-
<input type="text" name="name" value="Name">
181-
<input type="text" name="surname" value="Surname">
180+
<input type="text" name="surname" value="">
181+
<input type="text" name="name" value="이름">
182182
</form>
183183

184184
<style> .focused { outline: 1px solid red; } </style>
185185

186186
<script>
187187
*!*
188-
// put the handler on capturing phase (last argument true)
188+
// 캡쳐링 단계에서 핸들러가 트리거되도록 합니다(마지막 인수가 true)
189189
form.addEventListener("focus", () => form.classList.add('focused'), true);
190190
form.addEventListener("blur", () => form.classList.remove('focused'), true);
191191
*/!*
192192
</script>
193193
```
194194

195-
Second, there are `focusin` and `focusout` events -- exactly the same as `focus/blur`, but they bubble.
195+
두 번째 방법은 `focusin``focusout`을 이용하는 것입니다. 두 이벤트는 `focus`, `blur`와 동일하지만 버블링이 된다는 점에서 차이가 있습니다.
196196

197-
Note that they must be assigned using `elem.addEventListener`, not `on<event>`.
198-
199-
So here's another working variant:
197+
`focusin``focusout`을 사용할 때 주의할 점은 `on<event>` 방식으로 핸들러를 추가하면 안 되고 `elem.addEventListener` 방식으로 핸들러를 추가해야 한다는 점입니다.
200198

199+
예시를 실행해 실제 이벤트가 버블링 되는지 확인해봅시다.
201200
```html autorun height=80
202201
<form id="form">
203-
<input type="text" name="name" value="Name">
204-
<input type="text" name="surname" value="Surname">
202+
<input type="text" name="surname" value="">
203+
<input type="text" name="name" value="이름">
205204
</form>
206205

207206
<style> .focused { outline: 1px solid red; } </style>
@@ -214,12 +213,12 @@ So here's another working variant:
214213
</script>
215214
```
216215

217-
## Summary
216+
## 요약
218217

219-
Events `focus` and `blur` trigger on an element focusing/losing focus.
218+
`focus``blur` 이벤트는 각각 요소가 포커스를 받을 때, 잃을 때 발생합니다.
220219

221-
Their specials are:
222-
- They do not bubble. Can use capturing state instead or `focusin/focusout`.
223-
- Most elements do not support focus by default. Use `tabindex` to make anything focusable.
220+
두 이벤트를 사용할 땐 다음을 유의해야 합니다.
221+
- `focus``blur` 이벤트는 버블링 되지 않습니다. 캡처링이나 `focusin`, `focusout`을 사용하면 이벤트 위임 효과를 볼 수 있습니다.
222+
- 대부분의 요소는 기본적으로 포커스를 지원하지 않습니다. 그럼에도 불구하고 포커스 하고 싶은 요소가 있다면 `tabindex`를 사용하면 됩니다.
224223

225-
The current focused element is available as `document.activeElement`.
224+
현재 포커스된 요소는 `document.activeElement`를 통해 확인할 수 있습니다.

0 commit comments

Comments
 (0)