You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 2-ui/4-forms-controls/2-focus-blur/4-edit-td-click/task.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,15 @@ importance: 5
2
2
3
3
---
4
4
5
-
# Edit TD on click
5
+
# 点击时编辑单元格
6
6
7
-
Make table cells editable on click.
7
+
让单元格在点击时可编辑。
8
8
9
-
-On click -- the cell should became "editable" (textarea appears inside), we can change HTML. There should be no resize, all geometry should remain the same.
10
-
-Buttons OK and CANCEL appear below the cell to finish/cancel the editing.
11
-
-Only one cell may be editable at a moment. While a `<td>`is in "edit mode", clicks on other cells are ignored.
12
-
-The table may have many cells. Use event delegation.
The only pitfall is that `keydown`only triggers on elements with focus. So we need to add `tabindex` to the element. As we're forbidden to change HTML, we can use `mouse.tabIndex`property for that.
Copy file name to clipboardExpand all lines: 2-ui/4-forms-controls/2-focus-blur/article.md
+57-57Lines changed: 57 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,25 @@
1
-
# Focusing: focus/blur
1
+
# 聚焦:focus/blur
2
2
3
-
An element receives a 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 into an element by default when a page loads and other means of getting a focus.
3
+
当一个元素被用户点击或使用键盘上的 `key:Tab`选中时,该元素会被聚焦。当网页加载时 HTML `autofocus`属性也可以让一个焦点落在元素上,不仅如此,还有其它途径可以获取焦点。
4
4
5
-
Focusing generally means: "prepare to accept the data here", so that's the moment when we can run the code to initialize or load something.
5
+
聚焦通常表示:“这里准备好接受数据了”,而这也正是我们运行代码去初始化或加载一些东西的时候。
6
6
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.
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
+
失去焦点通常表示:“数据已经完成输入了”,所以我们可以运行代码来检查它,甚至可以是保存到服务器上。
10
10
11
-
There are important peculiarities when working with focus events. We'll do the best to cover them here.
11
+
当操作聚焦事件的时候有一些重要的特性需要注意。我们会尽量在这里介绍。
12
12
13
-
## Events focus/blur
13
+
## focus/blur 事件
14
14
15
-
The`focus`event is called on focusing, and `blur`-- when the element loses the focus.
15
+
当元素聚焦时它的`focus`事件被触发,还有当元素失去焦点的时候它的 `blur`事件被触发。
16
16
17
-
Let's use them for validation of an input field.
17
+
让我们使用它们去校验一个输入字段。
18
18
19
-
In the example below:
19
+
在下面的例子中:
20
20
21
-
-The `blur`handler checks if the field the email is 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`事件处理器还会再检查一遍):
23
23
24
24
```html run autorun height=60
25
25
<style>
@@ -49,14 +49,14 @@ Your email please: <input type="email" id="input">
49
49
</script>
50
50
```
51
51
52
-
Modern HTML allows 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 on the server if it's correct.
52
+
在现代的 HTML 中,可以使用 `required`、`pattern`等诸多输入属性校验表单输入内容,并且这些属性在很多时候满足了我们的使用需求。JavaScript 可以让我们以更灵活的方式去实现。如果数据是正确的,我们可以把它自动发送到服务器上。
53
53
54
54
55
-
## Methods focus/blur
55
+
## focus/blur 方法
56
56
57
-
Methods`elem.focus()`and`elem.blur()`set/unset the focus on the element.
57
+
方法`elem.focus()`和`elem.blur()`可以设置和移除元素上的焦点。
58
58
59
-
For instance, let's make the visitor unable to leave the input if the value is invalid:
59
+
举个例子,如果输入值无效,我们可以让焦点一直保留在这个输入域上:
60
60
61
61
```html run autorun height=80
62
62
<style>
@@ -84,46 +84,46 @@ Your email please: <input type="email" id="input">
84
84
</script>
85
85
```
86
86
87
-
It works in all browsers except Firefox ([bug](https://bugzilla.mozilla.org/show_bug.cgi?id=53579)).
Please note that we can't "prevent losing focus" by calling `event.preventDefault()`in`onblur`, because `onblur` works *after* the element lost the focus.
One of them is when the visitor clicks somewhere else. But also JavaScript itself may cause it, for instance:
96
+
其中之一是用户点击了其它的地方。当然 JavaScript 本身也会导致这种事情发生,举个例子:
97
97
98
-
- An `alert` moves focus to itself, so it causes the focus loss at the element (`blur` event), and when the `alert` is dismissed, the focus comes back (`focus` event).
99
-
- If an element is removed from DOM, then it also causes the focus loss. If it is reinserted later, then the focus doesn't return.
The list varies 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
+
list 标签在不同的浏览器表现不同,但有一件事总是正确的:`focus/blur`保证支持那些用户可以交互的元素:比如 `<button>`、`<input>`、`<select>` 和 `<a>`等等。
110
110
111
-
From the other hand, elements that exist to format something like `<div>`, `<span>`, `<table>`-- are unfocusable by default. The method `elem.focus()`doesn't work on them, and `focus/blur`events are never triggered.
This can be changed using HTML-attribute `tabindex`.
113
+
使用 HTML 属性 `tabindex` 可以改变这种默认情况。
114
114
115
-
The purpose of this attribute is to specify the order number of the element when `key:Tab`is used to switch between them.
115
+
这个属性的目的是当使用 `key:Tab`在元素之间切换的时候指定它们的排列顺序。
116
116
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 us to the second one.
-`tabindex="-1"`means that `key:Tab`should ignore that element.
121
+
-`tabindex="0"`让元素成为最后一个。
122
+
-`tabindex="-1"`意味着 `key:Tab`应该忽略这个元素。
123
123
124
-
**Any element supports focusing if it has `tabindex`.**
124
+
**任何元素如果有属性 `tabindex`,它将会支持聚焦。**
125
125
126
-
For instance, here's a list. Click the first item and press `key:Tab`:
126
+
举个例子,这里有个列表。点击第一个项目然后按下 `key:Tab`:
127
127
128
128
```html autorun no-beautify
129
129
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 with the example.
@@ -140,17 +140,17 @@ Click the first item and press Tab. Keep track of the order. Please note that ma
140
140
</style>
141
141
```
142
142
143
-
The order is like this: `1 - 2 - 0` (zero is always the last). Normally, `<li>`does not support focusing, but `tabindex`full enables it, along with events and styling with `:focus`.
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.
0 commit comments