145 lines
4.3 KiB
Markdown
145 lines
4.3 KiB
Markdown
---
|
|
id: bad87fee1348bd9aed108826
|
|
title: Apunta a un hijo específico de un elemento usando jQuery
|
|
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--
|
|
|
|
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.
|
|
|
|
Afortunadamente, jQuery tiene algunos otros trucos para apuntar a los elementos correctos.
|
|
|
|
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.
|
|
|
|
Así es como le darías al tercer elemento en cada "well" la clase de rebote:
|
|
|
|
```js
|
|
$(".target:nth-child(3)").addClass("animated bounce");
|
|
```
|
|
|
|
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`.
|
|
|
|
# --hints--
|
|
|
|
El segundo elemento en tus elementos `target` deben rebotar.
|
|
|
|
```js
|
|
assert(
|
|
$('.target:nth-child(2)').hasClass('animated') &&
|
|
$('.target:nth-child(2)').hasClass('bounce')
|
|
);
|
|
```
|
|
|
|
Solo deben elegirse dos elementos.
|
|
|
|
```js
|
|
assert($('.animated.bounce').length === 2);
|
|
```
|
|
|
|
Debes usar el selector `:nth-child()` para modificar estos elementos.
|
|
|
|
```js
|
|
assert(code.match(/\:nth-child\(/g));
|
|
```
|
|
|
|
Solo debes usar jQuery para agregar estas clases al elemento.
|
|
|
|
```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>
|
|
```
|