--- 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

jQuery Playground

#left-well

#right-well

``` # --solutions-- ```html

jQuery Playground

#left-well

#right-well

```