2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aedf04756
|
|
|
|
|
challengeType: 0
|
2019-12-13 13:47:57 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cGJDQug'
|
|
|
|
|
forumTopicId: 18253
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: Class 选择器的优先级高于继承样式
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
"pink-text" class 覆盖了<code>body</code>元素的 CSS 声明。
|
|
|
|
|
我们刚刚证明了我们的 class 会覆盖<code>body</code>的 CSS 样式。那么,下一个问题是,我们要怎么样才能覆盖我们的<code>pink-text</code>class?
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
创建一个字体颜色为<code>blue</code>的<code>blue-text</code>CSS class,并确保它在<code>pink-text</code>下方声明。
|
|
|
|
|
在含有<code>pink-text</code>class 的<code>h1</code>元素里面,再添加一个<code>blue-text</code>class,这时候,我们将能看到到底是谁获胜。
|
|
|
|
|
HTML 同时应用多个 class 属性需以空格来间隔,例子如下:
|
|
|
|
|
<code>class="class1 class2"</code>
|
2020-03-03 22:41:15 +09:00
|
|
|
|
<strong>注意:</strong> HTML 元素里应用的 class 的先后顺序无关紧要。
|
2019-12-13 13:47:57 +08:00
|
|
|
|
但是,在<code><style></code>标签里面声明的<code>class</code>顺序十分重要。第二个声明始终优于第一个声明。因为<code>.blue-text</code>在<code>.pink-text</code>的后面声明,所以<code>.blue-text</code>会覆盖<code>.pink-text</code>的样式。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '<code>h1</code>元素应该包含<code>pink-text</code> class。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($("h1").hasClass("pink-text"));
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '<code>h1</code>元素应该包含<code>blue-text</code> class。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($("h1").hasClass("blue-text"));
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '<code>blue-text</code>和<code>pink-text</code>需同时应用于<code>h1</code>元素上。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($(".pink-text").hasClass("blue-text"));
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '<code>h1</code>元素的颜色应为蓝色。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($("h1").css("color") === "rgb(0, 0, 255)");
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='html-seed'>
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
body {
|
|
|
|
|
background-color: black;
|
|
|
|
|
font-family: monospace;
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
.pink-text {
|
|
|
|
|
color: pink;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1 class="pink-text">Hello World!</h1>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2019-12-13 13:47:57 +08:00
|
|
|
|
```html
|
2018-10-10 18:03:03 -04:00
|
|
|
|
// solution required
|
|
|
|
|
```
|
2019-12-13 13:47:57 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
|