--- id: bad87fee1348bd9aeda08826 title: 使用 jQuery id 選擇器選擇元素 challengeType: 6 forumTopicId: 18317 required: - link: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.css' dashedName: target-elements-by-id-using-jquery --- # --description-- 也能通過 id 屬性選取標籤。 首先,用 `$("#target3")` 選擇器選取 id 爲 `target3` 的 `button` 標籤。 注意,和 CSS 聲明一樣,在 id 名前需要添加 `#`。 然後,用 jQuery 的 `.addClass()` 方法添加 `animated` 和 `fadeOut` 類。 下面的代碼的效果是使 id 爲 `target6` 的 `button` 標籤淡出: ```js $("#target6").addClass("animated fadeOut"); ``` # --hints-- 應該選擇 `id` 爲 `target3` 的 `button` 元素,使用 jQuery 的 `addClass()` 函數給它添加 `animated` class。 ```js assert($('#target3').hasClass('animated')); ``` 應該選中 id 爲 `target3` 的元素,使用 jQuery 的 `addClass()` 函數給它添加 `fadeOut` class。 ```js assert( ($('#target3').hasClass('fadeOut') || $('#target3').hasClass('fadeout')) && code.match(/\$\(\s*.#target3.\s*\)/g) ); ``` 應該僅用 jQuery 給元素添加這些 class。 ```js assert(!code.match(/class.*animated/g)); ``` # --seed-- ## --seed-contents-- ```html

jQuery Playground

#left-well

#right-well

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

jQuery Playground

#left-well

#right-well

```