2021-06-15 00:49:18 -07:00
---
id: bad87fee1348bd9aed108826
2021-06-26 21:42:30 +05:30
title: Individuare il figlio specifico di un elemento usando jQuery
2021-06-15 00:49:18 -07:00
challengeType: 6
forumTopicId: 18315
required:
2021-06-26 21:42:30 +05:30
-
link: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.css'
2021-06-15 00:49:18 -07:00
dashedName: target-a-specific-child-of-an-element-using-jquery
---
# --description--
2021-06-26 21:42:30 +05:30
Hai già visto che gli attributi id sono molto convenienti per selezionare degli elementi con jQuery. Ma non avrai sempre degli id puliti con cui lavorare.
2021-06-15 00:49:18 -07:00
2021-06-26 21:42:30 +05:30
Fortunatamente, jQuery ha degli altri trucchi per la selezione degli elementi desiderati.
2021-06-15 00:49:18 -07:00
2021-06-26 21:42:30 +05:30
jQuery utilizza i selettori CSS per identificare gli elementi. Il selettore CSS `target:nth-child(n)` consente di selezionare tutti gli ennesimi elementi di una certa classe o tipo.
2021-06-15 00:49:18 -07:00
2021-06-26 21:42:30 +05:30
Ecco come potresti dare al terzo elemento in ogni well la classe bounce:
2021-06-15 00:49:18 -07:00
```js
$(".target:nth-child(3)").addClass("animated bounce");
```
2021-06-26 21:42:30 +05:30
Fai rimbalzare il secondo figlio in ciascuno dei tuoi elementi well. Devi selezionare i figli degli elementi con la classe `target` .
2021-06-15 00:49:18 -07:00
# --hints--
2021-06-26 21:42:30 +05:30
Il secondo elemento negli elementi `target` dovrebbe rimbalzare.
2021-06-15 00:49:18 -07:00
```js
assert(
$('.target:nth-child(2)').hasClass('animated') & &
$('.target:nth-child(2)').hasClass('bounce')
);
```
2021-06-26 21:42:30 +05:30
Solo due elementi dovrebbero rimbalzare.
2021-06-15 00:49:18 -07:00
```js
assert($('.animated.bounce').length === 2);
```
2021-06-26 21:42:30 +05:30
Dovresti usare il selettore `:nth-child()` per modificare questi elementi.
2021-06-15 00:49:18 -07:00
```js
assert(code.match(/\:nth-child\(/g));
```
2021-06-26 21:42:30 +05:30
Dovresti usare solo jQuery per aggiungere queste classi all'elemento.
2021-06-15 00:49:18 -07:00
```js
assert(
code.match(/\$\(".target:nth-child\(2\)"\)/g) ||
code.match(/\$\('.target:nth-child\(2\)'\)/g) ||
code.match(/\$\(".target"\).filter\(":nth-child\(2\)"\)/g) ||
code.match(/\$\('.target'\).filter\(':nth-child\(2\)'\)/g)
);
```
# --seed--
## --seed-contents--
```html
< script >
$(document).ready(function() {
$("#target1 ").css("color", "red");
$("#target1 ").prop("disabled", true);
$("#target4 ").remove();
$("#target2 ").appendTo("#right -well");
$("#target5 ").clone().appendTo("#left -well");
$("#target1 ").parent().css("background-color", "red");
$("#right -well").children().css("color", "orange");
});
< / script >
<!-- Only change code above this line -->
< div class = "container-fluid" >
< h3 class = "text-primary text-center" > jQuery Playground< / h3 >
< div class = "row" >
< div class = "col-xs-6" >
< h4 > #left -well</ h4 >
< div class = "well" id = "left-well" >
< button class = "btn btn-default target" id = "target1" > #target1 </ button >
< button class = "btn btn-default target" id = "target2" > #target2 </ button >
< button class = "btn btn-default target" id = "target3" > #target3 </ button >
< / div >
< / div >
< div class = "col-xs-6" >
< h4 > #right -well</ h4 >
< div class = "well" id = "right-well" >
< button class = "btn btn-default target" id = "target4" > #target4 </ button >
< button class = "btn btn-default target" id = "target5" > #target5 </ button >
< button class = "btn btn-default target" id = "target6" > #target6 </ button >
< / div >
< / div >
< / div >
< / div >
```
# --solutions--
```html
< script >
$(document).ready(function() {
$("#target1 ").css("color", "red");
$("#target1 ").prop("disabled", true);
$("#target4 ").remove();
$("#target2 ").appendTo("#right -well");
$("#target5 ").clone().appendTo("#left -well");
$("#target1 ").parent().css("background-color", "red");
$("#right -well").children().css("color", "orange");
$(".target:nth-child(2)").addClass("animated bounce");
});
< / script >
<!-- Only change code above this line -->
< div class = "container-fluid" >
< h3 class = "text-primary text-center" > jQuery Playground< / h3 >
< div class = "row" >
< div class = "col-xs-6" >
< h4 > #left -well</ h4 >
< div class = "well" id = "left-well" >
< button class = "btn btn-default target" id = "target1" > #target1 </ button >
< button class = "btn btn-default target" id = "target2" > #target2 </ button >
< button class = "btn btn-default target" id = "target3" > #target3 </ button >
< / div >
< / div >
< div class = "col-xs-6" >
< h4 > #right -well</ h4 >
< div class = "well" id = "right-well" >
< button class = "btn btn-default target" id = "target4" > #target4 </ button >
< button class = "btn btn-default target" id = "target5" > #target5 </ button >
< button class = "btn btn-default target" id = "target6" > #target6 </ button >
< / div >
< / div >
< / div >
< / div >
```