--- id: bad87fee1348bd9aed908626 title: 用多個 jQuery 選擇器選擇同一個元素 challengeType: 6 forumTopicId: 18322 required: - link: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.css' dashedName: target-the-same-element-with-multiple-jquery-selectors --- # --description-- 現在學寫了三種選取標籤的方法:用標籤選擇器: `$("button")`,用類選擇器:`$(".btn")` 以及用 id 選擇器:`$("#target1")` 。 雖然可以在單個 `.addClass()` 內添加多個類,但是我們可以用*三種不同的方式*給一種標籤添加類。 以三種不同的方式用 `.addClass()` 方法每次只給一種標籤添加一個類: 給所有的 `button` 標籤添加 `animated` 類。 給所有類爲 `.btn` 的 button 標籤添加 `shake` 類。 給所有 id 爲 `#target1` 的 button 標籤添加 `btn-primary` 類。 **注意:**只針對一個元素並且一次只能添加一個 class。 總之,三個選擇器最終將添加三個 class `shake`、`animated` 以及 `btn-primary` 到 `#target1` 上。 # --hints-- 應該使用 `$("button")` 選擇器。 ```js assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?button\s*?(?:'|")/gi)); ``` 應該使用 `$(".btn")` 選擇器。 ```js assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?\.btn\s*?(?:'|")/gi)); ``` 應該使用 `$("#target1")` 選擇器。 ```js assert(code.match(/\$\s*?\(\s*?(?:'|")\s*?#target1\s*?(?:'|")/gi)); ``` 三個選擇器每個應該只添加一個類。 ```js assert( code.match(/addClass/g) && code.match(/addClass\s*?\(\s*?('|")\s*?[\w-]+\s*?\1\s*?\)/g).length > 2 ); ``` `#target1` 標籤應具有 `animated`、`shake` 和 `btn-primary` 三個類。 ```js assert( $('#target1').hasClass('animated') && $('#target1').hasClass('shake') && $('#target1').hasClass('btn-primary') ); ``` 應該僅用 jQuery 給標籤添加類。 ```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

```