2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aedf06756
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 内联样式的优先级高于 ID 选择器
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 0
|
2019-12-13 13:47:57 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cGJDRha'
|
|
|
|
|
forumTopicId: 18252
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: override-class-declarations-with-inline-styles
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
我们刚刚证明了,id 选择器无论在 `style` 标签的任何位置声明,都会覆盖 class 声明的样式。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-02-06 04:42:36 +00:00
|
|
|
|
其实还有其他方法可以覆盖 CSS 样式。 你还记得行内样式吗?
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
|
|
|
|
|
2021-04-30 00:43:46 +09:00
|
|
|
|
使用行内样式尝试让 `h1` 的字体颜色变白。 记住,内联样式看起来是像这样:
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-21 10:58:20 -06:00
|
|
|
|
```html
|
|
|
|
|
<h1 style="color: green;">
|
|
|
|
|
```
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-03-21 10:58:20 -06:00
|
|
|
|
`h1` 元素应继续保留 `blue-text` 和 `pink-text` 这两个 class。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`h1` 元素应包含 `pink-text` class。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert($('h1').hasClass('pink-text'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`h1` 元素应包含 `blue-text` class。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert($('h1').hasClass('blue-text'));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-21 10:58:20 -06:00
|
|
|
|
`h1` 的 id 属性值应为 `orange-text`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('h1').attr('id') === 'orange-text');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`h1` 元素应含有行内样式。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(document.querySelector('h1[style]'));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
`h1` 元素的字体颜色应该为白色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('h1').css('color') === 'rgb(255, 255, 255)');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2019-12-13 13:47:57 +08:00
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
background-color: black;
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
#orange-text {
|
|
|
|
|
color: orange;
|
|
|
|
|
}
|
|
|
|
|
.pink-text {
|
|
|
|
|
color: pink;
|
|
|
|
|
}
|
|
|
|
|
.blue-text {
|
|
|
|
|
color: blue;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1 id="orange-text" class="pink-text blue-text">Hello World!</h1>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
background-color: black;
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
#orange-text {
|
|
|
|
|
color: orange;
|
|
|
|
|
}
|
|
|
|
|
.pink-text {
|
|
|
|
|
color: pink;
|
|
|
|
|
}
|
|
|
|
|
.blue-text {
|
|
|
|
|
color: blue;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1 id="orange-text" class="pink-text blue-text" style="color: white">Hello World!</h1>
|
|
|
|
|
```
|