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/1-document/04-searching-elements-dom/article.md
+61-62Lines changed: 61 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -140,21 +140,21 @@ Par exemple :
140
140
141
141
## closest
142
142
143
-
*Ancestors* of an element 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.
143
+
Les *ancêtres* d'un élément sont : le parent, le parent du parent, son propre parent etc... Les ancêtres forment une chaîne de parents depuis l'élément jusqu'au sommet.
144
144
145
-
The method`elem.closest(css)`looks for the nearest ancestor that matches the CSS-selector. The`elem`itself is also included in the search.
145
+
La méthode`elem.closest(css)`cherche l'ancêtre le plus proche qui correspond au sélecteur CSS. L'élément`elem`est lui-même inclus dans la recherche.
146
146
147
-
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.
147
+
En d'autres mots, la méthode`closest`part de l'élément et remonte en regardant chacun des parents. S'il correspond au sélecteur, la recherche s'arrête et l'ancêtre est renvoyé.
148
148
149
-
For instance:
149
+
Par exemple :
150
150
151
151
```html run
152
-
<h1>Contents</h1>
152
+
<h1>Contenu</h1>
153
153
154
154
<divclass="contents">
155
155
<ulclass="book">
156
-
<liclass="chapter">Chapter 1</li>
157
-
<liclass="chapter">Chapter 2</li>
156
+
<liclass="chapter">Chapître 1</li>
157
+
<liclass="chapter">Chapître 2</li>
158
158
</ul>
159
159
</div>
160
160
@@ -164,44 +164,44 @@ For instance:
164
164
alert(chapter.closest('.book')); // UL
165
165
alert(chapter.closest('.contents')); // DIV
166
166
167
-
alert(chapter.closest('h1')); // null (because h1 is not an ancestor)
167
+
alert(chapter.closest('h1')); // null (car h1 n'est pas un ancêtre)
168
168
</script>
169
169
```
170
170
171
171
## getElementsBy*
172
172
173
-
There are also other methods to look for nodes by a tag, class, etc.
173
+
Il y a aussi d'autres méthodes pour rechercher des balises par tag, classe, etc...
174
174
175
-
Today, they are mostly history, as`querySelector`is more powerful and shorter to write.
175
+
Aujourd'hui, elles sont principalement de l'histoire ancienne, car`querySelector`est plus puissante et plus courte à écrire.
176
176
177
-
So here we cover them mainly for completeness, while you can still find them in the old scripts.
177
+
Donc ici, on va surtout en parler dans le souci d'être complet, comme elles peuvent encore se retrouver dans des vieux scripts.
178
178
179
-
-`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".
180
-
-`elem.getElementsByClassName(className)`returns elements that have the given CSS class.
181
-
-`document.getElementsByName(name)`returns elements with the given `name` attribute, document-wide. Very rarely used.
179
+
-`elem.getElementsByTagName(tag)`cherche les éléments avec le tag donné et renvoie l'ensemble de ces éléments. Le paramètre `tag`peut aussi être une étoile `"*"`pour signifier n'importe quel tag.
180
+
-`elem.getElementsByClassName(className)`renvoie les éléments qui ont la classe CSS donnée.
181
+
-`document.getElementsByName(name)`renvoie les éléments qui ont l'attribut `name`, dans tout le document. Très rarement utilisé.
182
182
183
-
For instance:
183
+
Par exemple:
184
184
```js
185
-
//get all divs in the document
185
+
//récupérer tous les divs du document.
186
186
let divs =document.getElementsByTagName('div');
187
187
```
188
188
189
-
Let's find all `input`tags inside the table:
189
+
Trouvons tous les tags `input`dans le tableau :
190
190
191
191
```html run height=50
192
192
<tableid="table">
193
193
<tr>
194
-
<td>Your age:</td>
194
+
<td>Votre âge:</td>
195
195
196
196
<td>
197
197
<label>
198
-
<inputtype="radio"name="age"value="young"checked> less than 18
198
+
<inputtype="radio"name="age"value="young"checked> moins de 18
199
199
</label>
200
200
<label>
201
-
<inputtype="radio"name="age"value="mature"> from 18 to 50
201
+
<inputtype="radio"name="age"value="mature"> entre 18 et 50
202
202
</label>
203
203
<label>
204
-
<inputtype="radio"name="age"value="senior"> more than 60
204
+
<inputtype="radio"name="age"value="senior"> plus de 60
205
205
</label>
206
206
</td>
207
207
</tr>
@@ -218,31 +218,30 @@ Let's find all `input` tags inside the table:
218
218
</script>
219
219
```
220
220
221
-
```warn header="Don't forget the `\"s\"`letter!"
222
-
Novice developers sometimes forget the letter `"s"`. That is, they try to call `getElementByTagName`instead of <code>getElement<b>s</b>ByTagName</code>.
221
+
```warn header="N'oubliez pas la lettre `\"s\"` !"
222
+
Les développeurs junior oublient parfois la lettre `"s"`. Ils essaient donc d'appeler `getElementByTagName`au lieu de <code>getElement<b>s</b>ByTagName</code>.
223
223
224
-
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.
224
+
La lettre `"s"` letter n'apparait pas dans`getElementById`, car cette méthode renvoie un seul élément. Mais`getElementsByTagName`renvoie un ensemble d'éléments, il y a donc un `"s"`.
225
225
```
226
226
227
-
````warn header="It returns a collection, not an element!"
228
-
Another widespread novice mistake is to write:
227
+
````warn header="Elle renvoie un ensemble, pas un élément !"
228
+
Une autre erreur répandue parmi les débutants est d'écrire :
229
229
230
230
```js
231
-
// doesn't work
231
+
// ne fonctionne pas :
232
232
document.getElementsByTagName('input').value = 5;
233
233
```
234
234
235
-
That won't work, because it takes a *collection* of inputs and assigns the value to it rather than to elements inside it.
236
-
237
-
We should either iterate over the collection or get an element by its index, and then assign, like this:
235
+
Cela ne va pas marcher, parce qu'on essaie d'affecter une valeur à un ensemble d'objets plutôt qu'à un élément de cet ensemble.
236
+
On devrait plutôt itérer sur l'ensemble ou récupérer un élément par son index, et lui affecter la valeur, comme ceci :
@@ -251,26 +250,26 @@ Looking for `.article` elements:
251
250
</form>
252
251
253
252
<script>
254
-
// find by name attribute
253
+
// recherche par attribut nom
255
254
let form = document.getElementsByName('my-form')[0];
256
255
257
-
// find by class inside the form
256
+
// recherche par classe dans le formulaire
258
257
let articles = form.getElementsByClassName('article');
259
-
alert(articles.length); // 2, found two elements with class "article"
258
+
alert(articles.length); // 2 éléments trouvés avec la classe 'article'
260
259
</script>
261
260
```
262
261
263
-
## Live collections
262
+
## Ensembles courants
264
263
265
-
All methods `"getElementsBy*"` return a *live* collection. Such collections always reflect the current state of the document and "auto-update" when it changes.
264
+
Toutes les méthodes `"getElementsBy*"` renvoient l'ensemble *courant*. De tels ensembles montrent toujours l'état courant du document and se mettent à jour automatiquement quand celui-ci change.
266
265
267
-
In the example below, there are two scripts.
266
+
Dans l'exemple ci-dessous, il y a deux scripts :
268
267
269
-
1. The first one creates a reference to the collection of `<div>`. As of now, its length is `1`.
270
-
2. The second scripts runs after the browser meets one more `<div>`, so its length is `2`.
268
+
1. Le premier crée une référence à l'ensemble des `<div>`. Maintenant, sa longueur est `1`.
269
+
2. Le second se lance après que le navigateur aie rencontré un autre `<div>`, donc sa longueur est `2`.
271
270
272
271
```html run
273
-
<div>First div</div>
272
+
<div>Premier div</div>
274
273
275
274
<script>
276
275
let divs = document.getElementsByTagName('div');
@@ -286,13 +285,13 @@ In the example below, there are two scripts.
286
285
</script>
287
286
```
288
287
289
-
In contrast, `querySelectorAll` returns a *static* collection. It's like a fixed array of elements.
288
+
A l'opposé, `querySelectorAll` renvoie un ensemble *statique*. C'est comme un tableau fixe d'éléments
290
289
291
-
If we use it instead, then both scripts output `1`:
290
+
Si on l'utilise, alors les deux scripts ci-dessus renvoient `1`:
292
291
293
292
294
293
```html run
295
-
<div>First div</div>
294
+
<div>Premier div</div>
296
295
297
296
<script>
298
297
let divs = document.querySelectorAll('div');
@@ -308,31 +307,31 @@ If we use it instead, then both scripts output `1`:
308
307
</script>
309
308
```
310
309
311
-
Now we can easily see the difference. The static collection did not increase after the appearance of a new `div` in the document.
310
+
Maintenant, on voit facilement la différence. L'ensemble statique ne s'est pas incrémenté après l'apparition d'un nouveau `div` dans le document.
312
311
313
-
## Summary
312
+
## Résumé
314
313
315
-
There are 6 main methods to search for nodes in DOM:
314
+
Il y a 6 principales méthodes pour rechercher des balises dans le DOM :
316
315
317
316
<table>
318
317
<thead>
319
318
<tr>
320
-
<td>Method</td>
321
-
<td>Searches by...</td>
322
-
<td>Can call on an element?</td>
323
-
<td>Live?</td>
319
+
<td>Méthode</td>
320
+
<td>Recherches par...</td>
321
+
<td>Peut appeler un élément ?</td>
322
+
<td>Courant ?</td>
324
323
</tr>
325
324
</thead>
326
325
<tbody>
327
326
<tr>
328
327
<td><code>querySelector</code></td>
329
-
<td>CSS-selector</td>
328
+
<td>sélecteur CSS</td>
330
329
<td>✔</td>
331
330
<td>-</td>
332
331
</tr>
333
332
<tr>
334
333
<td><code>querySelectorAll</code></td>
335
-
<td>CSS-selector</td>
334
+
<td>sélecteur CSS</td>
336
335
<td>✔</td>
337
336
<td>-</td>
338
337
</tr>
@@ -344,31 +343,31 @@ There are 6 main methods to search for nodes in DOM:
344
343
</tr>
345
344
<tr>
346
345
<td><code>getElementsByName</code></td>
347
-
<td><code>name</code></td>
346
+
<td><code>nom</code></td>
348
347
<td>-</td>
349
348
<td>✔</td>
350
349
</tr>
351
350
<tr>
352
351
<td><code>getElementsByTagName</code></td>
353
-
<td>tag or <code>'*'</code></td>
352
+
<td>tag ou <code>'*'</code></td>
354
353
<td>✔</td>
355
354
<td>✔</td>
356
355
</tr>
357
356
<tr>
358
357
<td><code>getElementsByClassName</code></td>
359
-
<td>class</td>
358
+
<td>classe</td>
360
359
<td>✔</td>
361
360
<td>✔</td>
362
361
</tr>
363
362
</tbody>
364
363
</table>
365
364
366
-
By far the most used are `querySelector` and `querySelectorAll`, but `getElement(s)By*` can be sporadically helpful or found in the old scripts.
365
+
De loin, les plus utilisées sont `querySelector` et `querySelectorAll`, mais `getElement(s)By*` peut être de temps en temps utile ou rencontrée dans de vieux scripts.
367
366
368
-
Besides that:
367
+
A part ça :
369
368
370
-
- There is `elem.matches(css)` to check if `elem` matches the given CSS selector.
371
-
- There is `elem.closest(css)` to look for the nearest ancestor that matches the given CSS-selector. The `elem` itself is also checked.
369
+
- Il y a `elem.matches(css)` qui vérifie si `elem` correspond au sélecteur CSS donné.
370
+
- Il y a `elem.closest(css)` qui va chercher l'ancêtre le plus proche qui correspond au sélecteur CSS donné.
372
371
373
-
And let's mention one more method here to check for the child-parent relationship, as it's sometimes useful:
374
-
- `elemA.contains(elemB)` returns true if `elemB` is inside `elemA` (a descendant of `elemA`) or when `elemA==elemB`.
372
+
Et on peut mentionner ici une autre méthode pour vérifier la relation parent-enfant, ce qui est parfois utile :
373
+
- `elemA.contains(elemB)` renvoie true si `elemB` est dans `elemA` (un descendant de `elemA`) ou quand `elemA==elemB`.
0 commit comments