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

```