145 lines
4.2 KiB
Markdown
145 lines
4.2 KiB
Markdown
![]() |
---
|
|||
|
id: bad87fee1348bd9aed108826
|
|||
|
title: 使用 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--
|
|||
|
|
|||
|
已經看到了 id 屬性對於 jQuery 選擇器選取標籤的便利, 但這並不適用於所有情景。
|
|||
|
|
|||
|
幸運的是,jQuery 有一些其他的方法可以選取正確的標籤。
|
|||
|
|
|||
|
jQuery 可以用 CSS 選擇器(CSS Selectors)選取標籤。 `target:nth-child(n)` CSS 選擇器可以選取指定 class 或者元素類型的的第 n 個標籤。
|
|||
|
|
|||
|
下面的代碼展示了給每個區域(well)的第 3 個標籤設置彈跳(bounce)動畫效果:
|
|||
|
|
|||
|
```js
|
|||
|
$(".target:nth-child(3)").addClass("animated bounce");
|
|||
|
```
|
|||
|
|
|||
|
給 well 元素的第二個子元素添加彈跳(bounce)動畫效果。 你必須選擇具有 `target` class 的元素子項。
|
|||
|
|
|||
|
# --hints--
|
|||
|
|
|||
|
`target` 元素中的第二個元素應該有彈跳(bounce)動畫效果。
|
|||
|
|
|||
|
```js
|
|||
|
assert(
|
|||
|
$('.target:nth-child(2)').hasClass('animated') &&
|
|||
|
$('.target:nth-child(2)').hasClass('bounce')
|
|||
|
);
|
|||
|
```
|
|||
|
|
|||
|
應該僅兩個元素有彈跳(bounce)動畫效果。
|
|||
|
|
|||
|
```js
|
|||
|
assert($('.animated.bounce').length === 2);
|
|||
|
```
|
|||
|
|
|||
|
你應該使用 `:nth-child()` 選擇器修改這些元素。
|
|||
|
|
|||
|
```js
|
|||
|
assert(code.match(/\:nth-child\(/g));
|
|||
|
```
|
|||
|
|
|||
|
應該僅用 jQuery 給元素添加 class。
|
|||
|
|
|||
|
```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>
|
|||
|
```
|