2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd8aedf06756
|
|
|
|
|
challengeType: 0
|
2019-12-13 13:47:57 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cRkpDhB'
|
|
|
|
|
forumTopicId: 18251
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: ID 选择器优先级高于 Class 选择器
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
我们刚刚证明了浏览器读取 CSS 是由上到下的。这就意味着,如果发生冲突,浏览器将会应用最后声明的样式。
|
|
|
|
|
不过我们还没结束,还有其他方法来覆盖 CSS 样式。你还记得 id 属性吗?
|
|
|
|
|
通过给<code>h1</code>元素添加 id 属性,来覆盖 class 属性定义的同名样式。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
给<code>h1</code>元素添加 id 属性,属性值为<code>orange-text</code>。设置方式如下:
|
|
|
|
|
<code><h1 id="orange-text"></code>
|
|
|
|
|
<code>h1</code>元素继续保留<code>blue-text</code>和<code>pink-text</code>class。
|
|
|
|
|
在<code>style</code>元素中创建名为<code>orange-text</code>的 id 选择器。例子如下:
|
|
|
|
|
|
|
|
|
|
```css
|
|
|
|
|
#brown-text {
|
|
|
|
|
color: brown;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
注意:无论在<code>pink-text</code>class 的上面或者下面声明,id 选择器的优先级总是会高于 class 选择器。
|
|
|
|
|
</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>h1</code>的 id 属性值为<code>orange-text</code>。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($("h1").attr("id") === "orange-text");
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '应该只有一个<code>h1</code>元素。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert(($("h1").length === 1));
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '创建名为<code>orange-text</code>的 id 选择器。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert(code.match(/#orange-text\s*{/gi));
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '不要在<code>h1</code>元素里面使用<code>style(行内样式)</code>。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert(!code.match(/<h1.*style.*>/gi));
|
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(255, 165, 0)");
|
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;
|
|
|
|
|
}
|
|
|
|
|
.blue-text {
|
|
|
|
|
color: blue;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
<h1 class="pink-text blue-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
|
|
|
|
|