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
That's unless we declare the same-named variable by our own:
27
+
除非我们自己声明同名变量:
28
28
29
29
```html run untrusted height=0
30
30
<divid="elem"></div>
@@ -36,11 +36,11 @@ That's unless we declare the same-named variable by our own:
36
36
</script>
37
37
```
38
38
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,所以变量的来源可能会很模糊。
40
40
41
-
The better alternative is to use a special method `document.getElementById(id)`.
41
+
选择特殊的方法,才是最好的选择:`document.getElementById(id)`。
42
42
43
-
For instance:
43
+
例如:
44
44
45
45
```html run
46
46
<divid="elem">
@@ -56,33 +56,33 @@ For instance:
56
56
</script>
57
57
```
58
58
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.
The `id` must be unique. There can be only one element in the document with the given `id`.
62
+
`id` 必须唯一,文档中给定的 `id` 只能有唯一一个元素。
63
63
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.
- `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".
This method is callable in the context of any DOM element.
83
+
此方法可以在任意 DOM 元素的上下文中调用。
84
84
85
-
Let's find all `input`tags inside the table:
85
+
让我们找出在表格中的所有 `input`标签:
86
86
87
87
```html run height=50
88
88
<tableid="table">
@@ -115,35 +115,35 @@ Let's find all `input` tags inside the table:
115
115
```
116
116
117
117
```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>.
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.
There are also other rarely used methods of this kind:
141
+
还有其他很少使用的方法:
142
142
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.
let form = document.getElementsByName('my-form')[0];
157
157
158
-
// find by class inside the form
158
+
// 按在表单中的类查找
159
159
let articles = form.getElementsByClassName('article');
160
160
alert(articles.length); // 2, found two elements with class "article"
161
161
</script>
162
162
```
163
163
164
164
## querySelectorAll [#querySelectorAll]
165
165
166
-
Now goes the heavy artillery.
166
+
现在将进行重要的内容
167
167
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.
Here we look for all `<li>` elements that are last children:
170
+
我们将查找所有为最后一个子元素的 `<li>` 元素:
171
171
172
172
```html run
173
173
<ul>
@@ -189,35 +189,35 @@ Here we look for all `<li>` elements that are last children:
189
189
</script>
190
190
```
191
191
192
-
This method is indeed powerful, because any CSS selector can be used.
192
+
因为可以使用任何 CSS 选择器,所以这种方法很有用。
193
193
194
194
```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).
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.
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`.
The method comes handy when we are iterating over elements (like in array or something) and trying to filter those that interest us.
211
+
当我们迭代元素(例如数组或者一些其他内容)并试图过滤那些我们感兴趣的元素时,这个方法会很方便。
212
212
213
-
For instance:
213
+
例如:
214
214
215
215
```html run
216
216
<a href="http://example.com/file.zip">...</a>
217
217
<a href="http://ya.ru">...</a>
218
218
219
219
<script>
220
-
// can be any collection instead of document.body.children
220
+
// 不一定是 document.body.children,也可以是任何集合
221
221
for (let elem of document.body.children) {
222
222
*!*
223
223
if (elem.matches('a[href$="zip"]')) {
@@ -230,15 +230,15 @@ For instance:
230
230
231
231
## closest
232
232
233
-
All elements that are directly above the given one are called its *ancestors*.
233
+
所有直接在给定元素之上的元素都被称为它的“祖先”。
234
234
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
+
换句话说,祖先是:父类,父类的父类,它的父类等。祖先们一起组成了从元素到顶端的父类链。
236
236
237
-
The method `elem.closest(css)` looks the nearest ancestor that matches the CSS-selector. The `elem` itself is also included in the search.
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.
alert(chapter.closest('h1')); // null (because h1 is not an ancestor)
259
+
alert(chapter.closest('h1')); // null(因为 h1 不是祖先)
260
260
</script>
261
261
```
262
262
263
-
## Live collections
263
+
## Live 集合
264
264
265
-
All methods `"getElementsBy*"` return a *live* collection. Such collections always reflect the current state of the document and "auto-update" when it changes.
If we use it instead, then both scripts output `1`:
291
+
如果我们使用它,那么两个脚本都会输出 `1`:
292
292
293
293
294
294
```html run
@@ -308,13 +308,13 @@ If we use it instead, then both scripts output `1`:
308
308
</script>
309
309
```
310
310
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` 后,静态集合并没有增加。
312
312
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 操作都会影响它们。很快我们就可以看到更多的细节。
314
314
315
-
## Summary
315
+
## 总结
316
316
317
-
There are 6 main methods to search for nodes in DOM:
317
+
有 6 种主要的方法,可以在 DOM 中进行搜素:
318
318
319
319
<table>
320
320
<thead>
@@ -365,14 +365,14 @@ There are 6 main methods to search for nodes in DOM:
365
365
</tbody>
366
366
</table>
367
367
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.
0 commit comments