2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aec908853
|
2021-03-05 02:55:32 +00:00
|
|
|
|
title: 给 Bootstrap 元素添加 id 属性
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 0
|
2020-09-07 08:17:39 +00:00
|
|
|
|
forumTopicId: 16639
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: add-id-attributes-to-bootstrap-elements
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-05 02:55:32 +00:00
|
|
|
|
回忆一下,除了可以给元素添加 class 属性,我们还可以给元素设置 `id` 属性。
|
2020-09-07 08:17:39 +00:00
|
|
|
|
|
2021-03-05 02:55:32 +00:00
|
|
|
|
每个元素的 id 都必须是唯一的,并且在一个页面中只能使用一次。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-05 02:55:32 +00:00
|
|
|
|
让我们为每个 class 为 `well` 的 `div` 元素添加一个唯一的 id。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-05 02:55:32 +00:00
|
|
|
|
记住,可以这样给一个元素设置 id:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
`<div class="well" id="center-well">`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-05 02:55:32 +00:00
|
|
|
|
把左边 well 的 id 设置为 `left-well`。 右边的 well 的 id 设置为 `right-well`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-05 02:55:32 +00:00
|
|
|
|
左边 `well` 的 id 应该为 `left-well`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
$('.col-xs-6').children('#left-well') &&
|
|
|
|
|
$('.col-xs-6').children('#left-well').length > 0
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-05 02:55:32 +00:00
|
|
|
|
右边的 `well` 的 id 应该为 `right-well`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
```js
|
|
|
|
|
assert(
|
|
|
|
|
$('.col-xs-6').children('#right-well') &&
|
|
|
|
|
$('.col-xs-6').children('#right-well').length > 0
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<div class="container-fluid">
|
|
|
|
|
<h3 class="text-primary text-center">jQuery Playground</h3>
|
|
|
|
|
<div class="row">
|
|
|
|
|
<div class="col-xs-6">
|
|
|
|
|
<div class="well">
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="col-xs-6">
|
|
|
|
|
<div class="well">
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```html
|
|
|
|
|
<div class="container-fluid">
|
|
|
|
|
<h3 class="text-primary text-center">jQuery Playground</h3>
|
|
|
|
|
<div class="row">
|
|
|
|
|
<div class="col-xs-6">
|
|
|
|
|
<div class="well" id="left-well">
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="col-xs-6">
|
|
|
|
|
<div class="well" id="right-well">
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
<button class="btn btn-default target"></button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
```
|