Skip to content

Commit e04beca

Browse files
Starriersleviding
authored andcommitted
Translation/searching (javascript-tutorial#144)
* 翻译完成 * 翻译完成 * 翻译完成 * 翻译完成 * 翻译更新:后代“” 翻译统一 * 翻译完成 * 翻译修改:第一次 * 翻译校对:第二次 * 校对信息修改 * 校对信息修改 * 校对信息修改完成 * Update article.md
1 parent 6b48e1c commit e04beca

File tree

5 files changed

+92
-92
lines changed

5 files changed

+92
-92
lines changed

2-ui/1-document/04-searching-elements-dom/1-find-elements/solution.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
There are many ways to do it.
1+
实现的方式有很多种。
22

3-
Here are some of them:
3+
以下是列举的一些方法:
44

55
```js
66
// 1. The table with `id="age-table"`.

2-ui/1-document/04-searching-elements-dom/1-find-elements/task.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ importance: 4
22

33
---
44

5-
# Search for elements
5+
# 搜索元素
66

7-
Here's the document with the table and form.
7+
这是带有表格和表单的文档。
88

9-
How to find?
9+
如何查找?
1010

11-
1. The table with `id="age-table"`.
12-
2. All `label` elements inside that table (there should be 3 of them).
13-
3. The first `td` in that table (with the word "Age").
14-
4. The `form` with the name `search`.
15-
5. The first `input` in that form.
16-
6. The last `input` in that form.
11+
1. `id="age-table"` 的表格。
12+
2. 所有的 `label` 元素都内嵌于表格(应该有三个)。
13+
3. 表格中的第一个 `td`(字段是 "Age")。
14+
4. `form` 的一个字段是 `search`
15+
5. 第一个 `input` 在表单中。
16+
6. 最后一个 `input` 在表单中。
1717

18-
Open the page [table.html](table.html) in a separate window and make use of browser tools for that.
18+
新开一个独立的窗口,打开页面 [table.html](table.html),然后再此页面上使用开发者工具。
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
Let's make a loop over `<li>`:
1+
我们在 `<li>` 中使用循环:
22

33
```js
44
for (let li of document.querySelectorAll('li')) {
55
...
66
}
77
```
88

9-
In the loop we need to get the text inside every `li`. We can read it directly from the first child node, that is the text node:
9+
循环时,我们需要获取每个 `li` 的文本下标。我们可以直接从第一个节点开始读取,这就是文本节点:
1010

1111
```js
1212
for (let li of document.querySelectorAll('li')) {
@@ -16,4 +16,4 @@ for (let li of document.querySelectorAll('li')) {
1616
}
1717
```
1818

19-
Then we can get the number of descendants `li.getElementsByTagName('li')`.
19+
之后我们就可以获取后代 `li.getElementsByTagName('li')` 的数目。

2-ui/1-document/04-searching-elements-dom/2-tree-info/task.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ importance: 5
22

33
---
44

5-
# Count descendants
5+
# 计算后代
66

7-
There's a tree structured as nested `ul/li`.
7+
构造用于嵌套 `ul/li` 的树结构。
88

9-
Write the code that for each `<li>` shows:
9+
为显示每个 `<li>` 而编写代码:
1010

11-
1. What's the text inside it (without the subtree)
12-
2. The number of nested `<li>` -- all descendants, including the deeply nested ones.
11+
1. 文本内容是什么(没有子树)。
12+
2. 嵌套 `<li>` 的数量 —— 所有的后代,包括嵌套的后代。
1313

1414
[demo src="solution"]

2-ui/1-document/04-searching-elements-dom/article.md

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# Searching: getElement* and querySelector*
1+
# 搜索: getElement* querySelector*
22

3-
DOM navigation properties are great when elements are close to each other. What if they are not? How to get an arbitrary element of the page?
3+
当元素关联较密切时,DOM 导航属性是最好的。万一不是这样该怎么办?如何去获取页面上的任意一个元素?
44

5-
There are additional searching methods for that.
6-
## document.getElementById or just id
5+
还有其他的搜索方法。
6+
## document.getElementById 或者只使用 id
77

8-
If an element has the `id` attribute, then there's a global variable by the name from that `id`.
8+
如果元素有 `id` 属性,那么该 `id` 也会有一个同名全局变量。
99

10-
We can use it to access the element, like this:
10+
我们可以用以下方式来访问元素:
1111

1212
```html run
1313
<div id="*!*elem*/!*">
@@ -18,13 +18,13 @@ We can use it to access the element, like this:
1818
alert(elem); // DOM-element with id="elem"
1919
alert(window.elem); // accessing global variable like this also works
2020
21-
// for elem-content things are a bit more complex
22-
// that has a dash inside, so it can't be a variable name
23-
alert(window['elem-content']); // ...but accessible using square brackets [...]
21+
// 对于 elem-content 会稍微有些复杂
22+
// 因为里面有破折号,所以不是一个变量名
23+
alert(window['elem-content']); // ...但可以使用方括号 [...]
2424
</script>
2525
```
2626

27-
That's unless we declare the same-named variable by our own:
27+
除非我们自己声明同名变量:
2828

2929
```html run untrusted height=0
3030
<div id="elem"></div>
@@ -36,11 +36,11 @@ That's unless we declare the same-named variable by our own:
3636
</script>
3737
```
3838

39-
The behavior is described [in the specification](http://www.whatwg.org/specs/web-apps/current-work/#dom-window-nameditem), but it is supported mainly for compatibility. The browser tries to help us by mixing namespaces of JS and DOM. Good for very simple scripts, but there may be name conflicts. Also, when we look in JS and don't have HTML in view, it's not obvious where the variable comes from.
39+
[在规范中](http://www.whatwg.org/specs/web-apps/current-work/#dom-window-nameditem)描述了这种行为,主要是考虑到兼容性才对它进行了支持。为了帮助我们,浏览器尝试了混合 JS DOM 的命名空间。但这仅仅对简单脚本有效,因为它们可能会产生命名冲突。同时,当我们在 JS 中查看时,因为无法在视图中查看 HTML,所以变量的来源可能会很模糊。
4040

41-
The better alternative is to use a special method `document.getElementById(id)`.
41+
选择特殊的方法,才是最好的选择:`document.getElementById(id)`
4242

43-
For instance:
43+
例如:
4444

4545
```html run
4646
<div id="elem">
@@ -56,33 +56,33 @@ For instance:
5656
</script>
5757
```
5858

59-
Here in the tutorial we'll often use `id` to directly reference an element, but that's only to keep things short. In real life `document.getElementById` is the preferred method.
59+
本教程中,我们经常使用 `id` 来直接引用属性,但这仅仅是为了简化。实际开发中,使用 `document.getElementById` 才是最佳选择。
6060

6161
```smart header="There can be only one"
62-
The `id` must be unique. There can be only one element in the document with the given `id`.
62+
`id` 必须唯一,文档中给定的 `id` 只能有唯一一个元素。
6363
64-
If there are multiple elements with the same `id`, then the behavior of corresponding methods is unpredictable. The browser may return any of them at random. So please stick to the rule and keep `id` unique.
64+
如果有多个元素具有同名 `id`,那么对应方法的行为将不可预测。浏览器将随机返回其他的一个。因此未来保证 `id` 的唯一性,请严格遵守规则。
6565
```
6666

6767
```warn header="Only `document.getElementById`, not `anyNode.getElementById`"
68-
The method `getElementById` that can be called only on `document` object. It looks for the given `id` in the whole document.
68+
`getElementById` 只能在 `document` 对象上调用。它会在整个文档中查找给定的 `id`
6969
```
7070
7171
## getElementsBy*
7272
73-
There are also other methods to look for nodes:
73+
也有其他的方法来搜索节点:
7474
75-
- `elem.getElementsByTagName(tag)` looks for elements with the given tag and returns the collection of them. The `tag` parameter can also be a star `"*"` for "any tags".
75+
- `elem.getElementsByTagName(tag)` 用给定的标签来查找元素,并返回它们的集合。`tag` 参数也可以是“任何标签”的 `"*"`
7676
77-
For instance:
77+
例如:
7878
```js
79-
// get all divs in the document
79+
// 获取所有在文档中的 div
8080
let divs = document.getElementsByTagName('div');
8181
```
8282

83-
This method is callable in the context of any DOM element.
83+
此方法可以在任意 DOM 元素的上下文中调用。
8484

85-
Let's find all `input` tags inside the table:
85+
让我们找出在表格中的所有 `input` 标签:
8686

8787
```html run height=50
8888
<table id="table">
@@ -115,35 +115,35 @@ Let's find all `input` tags inside the table:
115115
```
116116

117117
```warn header="Don't forget the `\"s\"` letter!"
118-
Novice developers sometimes forget the letter `"s"`. That is, they try to call `getElementByTagName` instead of <code>getElement<b>s</b>ByTagName</code>.
118+
初级开发者会忽略字符 `"s"`。也就是说,它们会调用 `getElementByTagName` 而不是 <code>getElement<b>s</b>ByTagName</code>
119119

120-
The `"s"` letter is absent in `getElementById`, because it returns a single element. But `getElementsByTagName` returns a collection of elements, so there's `"s"` inside.
120+
`"s"` 字符并不存在于 `getElementById`,因为它只返回单个元素。但是 `getElementsByTagName` 返回的是一个元素集合,所以 `"s"` 包含在内。
121121
```
122122
123123
````warn header="It returns a collection, not an element!"
124-
Another widespread novice mistake is to write:
124+
另一个普遍存在的错误写法是:
125125
126126
```js
127-
// doesn't work
127+
// 无法运行
128128
document.getElementsByTagName('input').value = 5;
129129
```
130130

131-
That won't work, because it takes a *collection* of inputs and assigns the value to it rather than to elements inside it.
131+
这无法运行,因为它接受输入的**集合**然后将值赋给它,而不是它里面的元素。
132132

133-
We should either iterate over the collection or get an element by its index, and then assign, like this:
133+
我们应该迭代集合或者按给定索引来获取元素,然后赋值,就像下述所示:
134134

135135
```js
136-
// should work (if there's an input)
136+
// 应该可以运行(如果有输入)
137137
document.getElementsByTagName('input')[0].value = 5;
138138
```
139139
````
140140
141-
There are also other rarely used methods of this kind:
141+
还有其他很少使用的方法:
142142
143-
- `elem.getElementsByClassName(className)` returns elements that have the given CSS class. Elements may have other classes too.
144-
- `document.getElementsByName(name)` returns elements with the given `name` attribute, document-wide. Exists for historical reasons, very rarely used, we mention it here only for completeness.
143+
- `elem.getElementsByClassName(className)` 返回具有给定 CSS 类的元素。元素也可能含有其他的类。
144+
- `document.getElementsByName(name)` 返回具有给定 `name` 属性的元素,文档范围。因为历史原因而很少使用。在这里提出,只是考虑到了完整性。
145145
146-
For instance:
146+
例如:
147147
148148
```html run height=50
149149
<form name="my-form">
@@ -152,22 +152,22 @@ For instance:
152152
</form>
153153
154154
<script>
155-
// find by name attribute
155+
// name 查找属性
156156
let form = document.getElementsByName('my-form')[0];
157157
158-
// find by class inside the form
158+
// 按在表单中的类查找
159159
let articles = form.getElementsByClassName('article');
160160
alert(articles.length); // 2, found two elements with class "article"
161161
</script>
162162
```
163163
164164
## querySelectorAll [#querySelectorAll]
165165
166-
Now goes the heavy artillery.
166+
现在将进行重要的内容
167167
168-
The call to `elem.querySelectorAll(css)` returns all elements inside `elem` matching the given CSS selector. That's the most often used and powerful method.
168+
`elem.querySelectorAll(css)` 的调用将返回与给定 CSS 选择器匹配 `elem` 中的所有元素。这是最常用和最有力的方法。
169169
170-
Here we look for all `<li>` elements that are last children:
170+
我们将查找所有为最后一个子元素的 `<li>` 元素:
171171
172172
```html run
173173
<ul>
@@ -189,35 +189,35 @@ Here we look for all `<li>` elements that are last children:
189189
</script>
190190
```
191191
192-
This method is indeed powerful, because any CSS selector can be used.
192+
因为可以使用任何 CSS 选择器,所以这种方法很有用。
193193
194194
```smart header="Can use pseudo-classes as well"
195-
Pseudo-classes in the CSS selector like `:hover` and `:active` are also supported. For instance, `document.querySelectorAll(':hover')` will return the collection with elements that the pointer is over now (in nesting order: from the outermost `<html>` to the most nested one).
195+
CSS 选择器的伪类,如 `:hover` `:active` 都是被支持的。例如,`document.querySelectorAll(':hover')` 将会返回指针现在已经结束的集合(按嵌套顺序:从最外层 `<html>` 到嵌套最多的元素)。
196196
```
197197
198198
199199
## querySelector [#querySelector]
200200
201-
The call to `elem.querySelector(css)` returns the first element for the given CSS selector.
201+
调用 `elem.querySelector(css)` 后,它会返回给定 CSS 选择器的第一个元素。
202202
203-
In other words, the result is the same as `elem.querySelectorAll(css)[0]`, but the latter is looking for *all* elements and picking one, while `elem.querySelector` just looks for one. So it's faster and shorter to write.
203+
换句话说,结果与 `elem.querySelectorAll(css)[0]` 相同,但是后者会从**所有**找到的元素中选取一个,而 `elem.querySelector` 只会查找一个。因此编写会更快更简洁。
204204
205205
## matches
206206
207-
Previous methods were searching the DOM.
207+
之前的方法是搜索 DOM 的。
208208
209-
The [elem.matches(css)](http://dom.spec.whatwg.org/#dom-element-matches) does not look for anything, it merely checks if `elem` matches the given CSS-selector. It returns `true` or `false`.
209+
[elem.matches(css)](http://dom.spec.whatwg.org/#dom-element-matches) 不会查找任何内容,它只会检查 `elem` 是否匹配给定的 CSS 选择器。它返回 `true` 或者 `false`
210210
211-
The method comes handy when we are iterating over elements (like in array or something) and trying to filter those that interest us.
211+
当我们迭代元素(例如数组或者一些其他内容)并试图过滤那些我们感兴趣的元素时,这个方法会很方便。
212212
213-
For instance:
213+
例如:
214214
215215
```html run
216216
<a href="http://example.com/file.zip">...</a>
217217
<a href="http://ya.ru">...</a>
218218
219219
<script>
220-
// can be any collection instead of document.body.children
220+
// 不一定是 document.body.children,也可以是任何集合
221221
for (let elem of document.body.children) {
222222
*!*
223223
if (elem.matches('a[href$="zip"]')) {
@@ -230,15 +230,15 @@ For instance:
230230
231231
## closest
232232
233-
All elements that are directly above the given one are called its *ancestors*.
233+
所有直接在给定元素之上的元素都被称为它的“祖先”。
234234
235-
In other words, ancestors are: parent, the parent of parent, its parent and so on. The ancestors together form the chain of parents from the element to the top.
235+
换句话说,祖先是:父类,父类的父类,它的父类等。祖先们一起组成了从元素到顶端的父类链。
236236
237-
The method `elem.closest(css)` looks the nearest ancestor that matches the CSS-selector. The `elem` itself is also included in the search.
237+
`elem.closest(css)` 方法会查找与 CSS 选择器匹配的最接近的祖先。`elem` 自己也会被搜索。
238238
239-
In other words, the method `closest` goes up from the element and checks each of parents. If it matches the selector, then the search stops, and the ancestor is returned.
239+
换句话说,方法 `closest` 在元素中得到了提升,并检查每个父类。如果与选择器匹配,则停止搜索并返回祖先。
240240
241-
For instance:
241+
例如:
242242
243243
```html run
244244
<h1>Contents</h1>
@@ -256,18 +256,18 @@ For instance:
256256
alert(chapter.closest('.book')); // UL
257257
alert(chapter.closest('.contents')); // DIV
258258
259-
alert(chapter.closest('h1')); // null (because h1 is not an ancestor)
259+
alert(chapter.closest('h1')); // null(因为 h1 不是祖先)
260260
</script>
261261
```
262262
263-
## Live collections
263+
## Live 集合
264264
265-
All methods `"getElementsBy*"` return a *live* collection. Such collections always reflect the current state of the document and "auto-update" when it changes.
265+
所有的 `"getElementsBy*"` 方法都会返回 **live** 集合。这类集合总是可以反映出文档的当前状态而且在文档变化时,可以自动更新。
266266
267-
In the example below, there are two scripts.
267+
下面的实例中,有两个脚本。
268268
269-
1. The first one creates a reference to the collection of `<div>`. As of now, it's length is `1`.
270-
2. The second scripts runs after the browser meets one more `<div>`, so it's length is `2`.
269+
1. 第一个方法创建了对集合 `<div>` 的引用。到目前为止,它的长度是 `1`
270+
2. 第二个脚本在浏览器再遇到一个 `<div>` 时,它的长度会变成 `2`
271271
272272
```html run
273273
<div>First div</div>
@@ -286,9 +286,9 @@ In the example below, there are two scripts.
286286
</script>
287287
```
288288
289-
In contrast, `querySelectorAll` returns a *static* collection. It's like a fixed array of elements.
289+
相反,`querySelectorAll` 会返回一个**static**集合。就像一个固定的元素数字。
290290
291-
If we use it instead, then both scripts output `1`:
291+
如果我们使用它,那么两个脚本都会输出 `1`
292292
293293
294294
```html run
@@ -308,13 +308,13 @@ If we use it instead, then both scripts output `1`:
308308
</script>
309309
```
310310
311-
Now we can easily see the difference. The static collection did not increase after the appearance of a new `div` in the document.
311+
现在我们可以很容易地看到不同之处。在文档中出现一个新的 `div` 后,静态集合并没有增加。
312312
313-
Here we used separate scripts to illustrate how the element addition affects the collection, but any DOM manipulations affect them. Soon we'll see more of them.
313+
我们在这里使用独立的脚本来说明元素添加是如何影响集合的,但是在此之后的任何 DOM 操作都会影响它们。很快我们就可以看到更多的细节。
314314
315-
## Summary
315+
## 总结
316316
317-
There are 6 main methods to search for nodes in DOM:
317+
有 6 种主要的方法,可以在 DOM 中进行搜素:
318318
319319
<table>
320320
<thead>
@@ -365,14 +365,14 @@ There are 6 main methods to search for nodes in DOM:
365365
</tbody>
366366
</table>
367367
368-
Please note that methods `getElementById` and `getElementsByName` can only be called in the context of the document: `document.getElementById(...)`. But not on an element: `elem.getElementById(...)` would cause an error.
368+
请注意,只有在文档 `document.getElementById(...)` 的上下文中才能调用 `getElementById` 和 `getElementsByName`。但元素中没有 `elem.getElementById(...)` 回报错。
369369
370-
Other methods can be called on elements too. For instance `elem.querySelectorAll(...)` will search inside `elem` (in the DOM subtree).
370+
也可以在元素上调用其他方法,例如 `elem.querySelectorAll(...)` 将会在 `elem`(在 DOM 子树中)内部进行搜素。
371371
372-
Besides that:
372+
除此以外:
373373
374-
- There is `elem.matches(css)` to check if `elem` matches the given CSS selector.
375-
- There is `elem.closest(css)` to look for the nearest ancestor that matches the given CSS-selector. The `elem` itself is also checked.
374+
- `elem.matches(css)` 用于检查 `elem` 与给定的 CSS 选择器是否匹配。
375+
- `elem.closest(css)` 用于查找与给定 CSS 选择器相匹配的最近的祖先。`elem` 本身也会被检查。
376376
377-
And let's mention one more method here to check for the child-parent relationship:
378-
- `elemA.contains(elemB)` returns true if `elemB` is inside `elemA` (a descendant of `elemA`) or when `elemA==elemB`.
377+
最后我们在提一种检查父子关系的方法:
378+
- 如果 `elemB` `elemA``elemA` 的后代)中或者当 `elemA==elemB` 时 `elemA.contains(elemB)` 将返回 true。

0 commit comments

Comments
 (0)