2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aedf08746
|
2021-01-08 19:20:48 +00:00
|
|
|
title: 从 body 元素继承样式
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 0
|
2019-12-13 05:47:57 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/c9bmdtR'
|
|
|
|
forumTopicId: 18204
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
我们已经证明每一个 HTML 页面都含有 `body` 元素,我们也可以在 `body` 元素上使用 CSS 样式。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
设置 `body` 元素样式的方法跟设置其他 HTML 元素样式的方式一样,并且其他元素也会继承 `body` 中所设置的样式。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
首先,创建一个内容文本为 `Hello World` 的 `h1` 元素。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
接着,在 `body` 的 CSS 规则里面添加 `color: green;`,这会将页面内所有字体的颜色都设置为绿色。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
最后,在 `body` 的 CSS 规则里面添加 `font-family: monospace;`,这会将页面内所有字体的 `font-family` 都设置为 `monospace`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
应创建一个 `h1` 元素。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert($('h1').length > 0);
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`h1` 元素的内容文本应为 `Hello World`。
|
2020-12-16 07:37:30 +00:00
|
|
|
|
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('h1').length > 0 &&
|
|
|
|
$('h1')
|
|
|
|
.text()
|
|
|
|
.match(/hello world/i)
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
确保 `h1` 元素具有结束标签。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
code.match(/<\/h1>/g) &&
|
|
|
|
code.match(/<h1/g) &&
|
|
|
|
code.match(/<\/h1>/g).length === code.match(/<h1/g).length
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`body` 元素的 `color` 属性值应为 `green`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('body').css('color') === 'rgb(0, 128, 0)');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`body` 元素的 `font-family` 属性值应为 `monospace`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('body')
|
|
|
|
.css('font-family')
|
|
|
|
.match(/monospace/i)
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`h1` 元素应该继承 `body` 的 `monospace` 字体属性。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
$('h1').length > 0 &&
|
|
|
|
$('h1')
|
|
|
|
.css('font-family')
|
|
|
|
.match(/monospace/i)
|
|
|
|
);
|
|
|
|
```
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2021-01-08 19:20:48 +00:00
|
|
|
`h1` 元素的字体颜色应继承 `body` 元素所设置的绿色。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('h1').length > 0 && $('h1').css('color') === 'rgb(0, 128, 0)');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-02-17 16:40:55 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|