Skip to content

Commit e8c90b7

Browse files
committed
Finished version
1 parent 603b8a4 commit e8c90b7

File tree

1 file changed

+61
-62
lines changed
  • 2-ui/1-document/04-searching-elements-dom

1 file changed

+61
-62
lines changed

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

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -140,21 +140,21 @@ Par exemple :
140140

141141
## closest
142142

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.
144144

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.
146146

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é.
148148

149-
For instance:
149+
Par exemple :
150150

151151
```html run
152-
<h1>Contents</h1>
152+
<h1>Contenu</h1>
153153

154154
<div class="contents">
155155
<ul class="book">
156-
<li class="chapter">Chapter 1</li>
157-
<li class="chapter">Chapter 2</li>
156+
<li class="chapter">Chapître 1</li>
157+
<li class="chapter">Chapître 2</li>
158158
</ul>
159159
</div>
160160

@@ -164,44 +164,44 @@ For instance:
164164
alert(chapter.closest('.book')); // UL
165165
alert(chapter.closest('.contents')); // DIV
166166
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)
168168
</script>
169169
```
170170

171171
## getElementsBy*
172172

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...
174174

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.
176176

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.
178178

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é.
182182

183-
For instance:
183+
Par exemple:
184184
```js
185-
// get all divs in the document
185+
// récupérer tous les divs du document.
186186
let divs = document.getElementsByTagName('div');
187187
```
188188

189-
Let's find all `input` tags inside the table:
189+
Trouvons tous les tags `input` dans le tableau :
190190

191191
```html run height=50
192192
<table id="table">
193193
<tr>
194-
<td>Your age:</td>
194+
<td>Votre âge:</td>
195195

196196
<td>
197197
<label>
198-
<input type="radio" name="age" value="young" checked> less than 18
198+
<input type="radio" name="age" value="young" checked> moins de 18
199199
</label>
200200
<label>
201-
<input type="radio" name="age" value="mature"> from 18 to 50
201+
<input type="radio" name="age" value="mature"> entre 18 et 50
202202
</label>
203203
<label>
204-
<input type="radio" name="age" value="senior"> more than 60
204+
<input type="radio" name="age" value="senior"> plus de 60
205205
</label>
206206
</td>
207207
</tr>
@@ -218,31 +218,30 @@ Let's find all `input` tags inside the table:
218218
</script>
219219
```
220220

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>.
223223

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"`.
225225
```
226226
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 :
229229
230230
```js
231-
// doesn't work
231+
// ne fonctionne pas :
232232
document.getElementsByTagName('input').value = 5;
233233
```
234234

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 :
238237

239238
```js
240-
// should work (if there's an input)
239+
// doit fonctionner (s'il y a un élément 'input' )
241240
document.getElementsByTagName('input')[0].value = 5;
242241
```
243242
````
244243
245-
Looking for `.article` elements:
244+
Recherche des éléments `.article` :
246245
247246
```html run height=50
248247
<form name="my-form">
@@ -251,26 +250,26 @@ Looking for `.article` elements:
251250
</form>
252251
253252
<script>
254-
// find by name attribute
253+
// recherche par attribut nom
255254
let form = document.getElementsByName('my-form')[0];
256255
257-
// find by class inside the form
256+
// recherche par classe dans le formulaire
258257
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'
260259
</script>
261260
```
262261
263-
## Live collections
262+
## Ensembles courants
264263
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.
266265
267-
In the example below, there are two scripts.
266+
Dans l'exemple ci-dessous, il y a deux scripts :
268267
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`.
271270
272271
```html run
273-
<div>First div</div>
272+
<div>Premier div</div>
274273
275274
<script>
276275
let divs = document.getElementsByTagName('div');
@@ -286,13 +285,13 @@ In the example below, there are two scripts.
286285
</script>
287286
```
288287
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
290289
291-
If we use it instead, then both scripts output `1`:
290+
Si on l'utilise, alors les deux scripts ci-dessus renvoient `1`:
292291
293292
294293
```html run
295-
<div>First div</div>
294+
<div>Premier div</div>
296295
297296
<script>
298297
let divs = document.querySelectorAll('div');
@@ -308,31 +307,31 @@ If we use it instead, then both scripts output `1`:
308307
</script>
309308
```
310309
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.
312311
313-
## Summary
312+
## Résumé
314313
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 :
316315
317316
<table>
318317
<thead>
319318
<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>
324323
</tr>
325324
</thead>
326325
<tbody>
327326
<tr>
328327
<td><code>querySelector</code></td>
329-
<td>CSS-selector</td>
328+
<td>sélecteur CSS</td>
330329
<td>✔</td>
331330
<td>-</td>
332331
</tr>
333332
<tr>
334333
<td><code>querySelectorAll</code></td>
335-
<td>CSS-selector</td>
334+
<td>sélecteur CSS</td>
336335
<td>✔</td>
337336
<td>-</td>
338337
</tr>
@@ -344,31 +343,31 @@ There are 6 main methods to search for nodes in DOM:
344343
</tr>
345344
<tr>
346345
<td><code>getElementsByName</code></td>
347-
<td><code>name</code></td>
346+
<td><code>nom</code></td>
348347
<td>-</td>
349348
<td>✔</td>
350349
</tr>
351350
<tr>
352351
<td><code>getElementsByTagName</code></td>
353-
<td>tag or <code>'*'</code></td>
352+
<td>tag ou <code>'*'</code></td>
354353
<td>✔</td>
355354
<td>✔</td>
356355
</tr>
357356
<tr>
358357
<td><code>getElementsByClassName</code></td>
359-
<td>class</td>
358+
<td>classe</td>
360359
<td>✔</td>
361360
<td>✔</td>
362361
</tr>
363362
</tbody>
364363
</table>
365364
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.
367366
368-
Besides that:
367+
A part ça :
369368
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é.
372371
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

Comments
 (0)