2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aeda08826
|
2020-12-16 07:37:30 +00:00
|
|
|
title: 使用 jQuery 配合 id 选择器选择元素
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 6
|
2020-09-07 08:17:51 +00:00
|
|
|
forumTopicId: 18317
|
2020-12-16 07:37:30 +00:00
|
|
|
required:
|
|
|
|
- link: 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.0/animate.css'
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2020-09-07 08:17:51 +00:00
|
|
|
你也能通过 id 属性选取标签。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
首先,用`$("#target3")`选择器选取 id 为`target3`的`button`标签。
|
|
|
|
|
|
|
|
注意,和 CSS 属性一样,在 id 名前需要添加`#`。
|
|
|
|
|
|
|
|
然后,用 jQuery 的`.addClass()`方法添加`animated`和`fadeOut`类。
|
|
|
|
|
|
|
|
下面的代码的效果是使 id 为`target6`的`button`标签淡出:
|
|
|
|
|
|
|
|
`$("#target6").addClass("animated fadeOut")`.
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
|
|
用 jQuery 的`addClass()`方法给`id`为`target3`的`button`标签添加`animated`类。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('#target3').hasClass('animated'));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
用 jQuery 的`addClass()`方法给`id`为`target3`的标签的类添加`fadeOut`类。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
($('#target3').hasClass('fadeOut') || $('#target3').hasClass('fadeout')) &&
|
|
|
|
code.match(/\$\(\s*.#target3.\s*\)/g)
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
仅用 jQuery 给标签设置类。
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(!code.match(/class.*animated/g));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|