Files
freeCodeCamp/curriculum/challenges/portuguese/03-front-end-development-libraries/jquery/target-a-specific-child-of-an-element-using-jquery.md

145 lines
4.3 KiB
Markdown
Raw Permalink Normal View History

---
id: bad87fee1348bd9aed108826
title: Mirar um filho específico de um elemento usando o jQuery
challengeType: 6
forumTopicId: 18315
required:
2021-07-09 21:23:54 -07:00
-
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--
Você já viu o motivo de mirar os atributos de id ser tão conveniente com seletores jQuery. Mas você nem sempre terá ids legais para trabalhar.
Felizmente, o jQuery possui outros truques para apontar para os elementos certos.
O jQuery usa seletores CSS para apontar elementos. O seletor CSS `target:nth-child(n)` o permite selecionar todos os enésimos (nth) elementos com a classe ou tipo de elemento alvos.
Aqui está como você daria ao terceiro elemento em cada well a classe bounce:
```js
$(".target:nth-child(3)").addClass("animated bounce");
```
Faça quicar o segundo filho em cada um dos seus elementos well. Você deve selecionar os filhos do elemento com a classe `target`.
# --hints--
O segundo elemento nos seus elementos `target` deve quicar.
```js
assert(
$('.target:nth-child(2)').hasClass('animated') &&
$('.target:nth-child(2)').hasClass('bounce')
);
```
Apenas dois elementos devem quicar.
```js
assert($('.animated.bounce').length === 2);
```
Você deve usar o seletor `:nth-child()` para modificar esses elementos.
```js
assert(code.match(/\:nth-child\(/g));
```
Você deve usar apenas jQuery para adicionar essas classes ao 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>
```