2021-02-06 04:42:36 +00:00
---
id: bad87fee1348bd9aed108826
2021-05-24 00:42:08 -07:00
title: Apunta a un hijo específico de un elemento usando jQuery
2021-02-06 04:42:36 +00:00
challengeType: 6
forumTopicId: 18315
required:
-
link: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.css'
dashedName: target-a-specific-child-of-an-element-using-jquery
---
# --description--
2021-05-24 00:42:08 -07:00
Has visto por qué los atributos id son muy convenientes para apuntar con los selectores de jQuery. Pero no siempre tendrás tan buenos id's con los que trabajar.
2021-02-06 04:42:36 +00:00
2021-05-24 00:42:08 -07:00
Afortunadamente, jQuery tiene algunos otros trucos para apuntar a los elementos correctos.
2021-02-06 04:42:36 +00:00
2021-05-24 00:42:08 -07:00
jQuery usa selectores de CSS para apuntar elementos. El selector `target:nth-child(n)` de CSS, te permite seleccionar todos los enésimos elementos con el tipo de elemento o la clase apuntada.
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
Así es como le darías al tercer elemento en cada "well" la clase de rebote:
2021-02-06 04:42:36 +00:00
2021-05-24 00:42:08 -07:00
```js
$(".target:nth-child(3)").addClass("animated bounce");
```
2021-02-06 04:42:36 +00:00
2021-09-07 07:47:37 -07:00
Haz que el segundo niño en cada uno de sus elementos de "well" rebote. Debes seleccionar los hijos de los elementos con la clase `target` .
2021-02-06 04:42:36 +00:00
# --hints--
2021-05-24 00:42:08 -07:00
El segundo elemento en tus elementos `target` deben rebotar.
2021-02-06 04:42:36 +00:00
```js
assert(
$('.target:nth-child(2)').hasClass('animated') & &
$('.target:nth-child(2)').hasClass('bounce')
);
```
2021-05-24 00:42:08 -07:00
Solo deben elegirse dos elementos.
2021-02-06 04:42:36 +00:00
```js
assert($('.animated.bounce').length === 2);
```
2021-05-24 00:42:08 -07:00
Debes usar el selector `:nth-child()` para modificar estos elementos.
2021-02-06 04:42:36 +00:00
```js
assert(code.match(/\:nth-child\(/g));
```
2021-09-07 07:47:37 -07:00
Solo debes usar jQuery para agregar estas clases al elemento.
2021-02-06 04:42:36 +00: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 >
```