2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: bad87fee1348bd9aedf08805
|
|
|
|
challengeType: 0
|
2019-12-13 05:47:57 +00:00
|
|
|
videoUrl: 'https://scrimba.com/c/cJKMBT2'
|
|
|
|
forumTopicId: 18349
|
2020-10-01 15:54:21 +00:00
|
|
|
title: 使用元素选择器来设置元素的样式
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2019-12-13 05:47:57 +00:00
|
|
|
<section id='description'>
|
|
|
|
在 CSS 中,页面样式的属性有几百个,但常用的不过几十个。
|
|
|
|
通过行内样式<code><h2 style="color: red;">CatPhotoApp</h2></code>,就可以修改<code>h2</code>元素的颜色为红色。
|
|
|
|
当我们只需要改变元素的某个样式时,行内样式最简单直观。当我们需要同时改变元素的很多样式时,<code>层叠样式表</code>往往是一个更好的选择。
|
|
|
|
在代码的顶部,创建一个<code>style</code>声明区域,如下方所示:
|
|
|
|
|
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
</style>
|
|
|
|
```
|
|
|
|
|
|
|
|
在<code>style</code>样式声明区域内,可以创建一个<code>元素选择器</code>,应用于所有的<code>h2</code>元素。例如,如果你想所有<code>h2</code>元素变成红色,可以添加下方的样式规则:
|
|
|
|
|
|
|
|
```html
|
|
|
|
<style>
|
|
|
|
h2 {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
```
|
|
|
|
|
|
|
|
注意,在每个元素的样式声明区域里,左右花括号(<code>{</code> 和 <code>}</code>)一定要写全。你需要确保所有样式规则位于花括号之间,并且每条样式规则都以分号结束。
|
|
|
|
</section>
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
## Instructions
|
2019-12-13 05:47:57 +00:00
|
|
|
<section id='instructions'>
|
|
|
|
删除<code>h2</code>元素的行内样式,然后创建<code>style</code>样式声明区域,最后添加 CSS 样式规则使<code>h2</code>元素变为蓝色。
|
|
|
|
</section>
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-03-03 13:41:15 +00:00
|
|
|
- text: 删除<code>h2</code>元素的行内样式。
|
2020-01-08 04:40:17 +00:00
|
|
|
testString: assert(!$("h2").attr("style"));
|
2020-03-03 13:41:15 +00:00
|
|
|
- text: 创建一个<code>style</code>样式声明区域。
|
2020-01-08 04:40:17 +00:00
|
|
|
testString: assert($("style") && $("style").length >= 1);
|
2020-03-03 13:41:15 +00:00
|
|
|
- text: <code>h2</code>元素颜色应为蓝色。
|
2020-01-08 04:40:17 +00:00
|
|
|
testString: assert($("h2").css("color") === "rgb(0, 0, 255)");
|
2020-03-03 13:41:15 +00:00
|
|
|
- text: 确保<code>h2</code>选择器的内容被花括号所围绕,并且样式规则以分号结束。
|
|
|
|
testString: assert(code.match(/h2\s*\{\s*color\s*:.*;\s*\}/g));
|
|
|
|
- text: 所有<code>style</code>应该是有效的且有一个结束标签。
|
|
|
|
testString: assert(code.match(/<\/style>/g) && code.match(/<\/style>/g).length === (code.match(/<style((\s)*((type|media|scoped|title|disabled)="[^"]*")?(\s)*)*>/g) || []).length);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='html-seed'>
|
|
|
|
|
|
|
|
```html
|
2019-03-13 19:38:25 +00:00
|
|
|
<h2 style="color: red;">CatPhotoApp</h2>
|
2018-10-10 22:03:03 +00:00
|
|
|
<main>
|
2019-12-13 05:47:57 +00:00
|
|
|
<p class="red-text">点击查看更多<a href="#">猫图</a>.</p>
|
|
|
|
|
|
|
|
<a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="一只仰卧着的萌猫"></a>
|
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
<div>
|
2019-12-13 05:47:57 +00:00
|
|
|
<p>猫咪最喜欢的三件东西:</p>
|
2018-10-10 22:03:03 +00:00
|
|
|
<ul>
|
2019-12-13 05:47:57 +00:00
|
|
|
<li>猫薄荷</li>
|
|
|
|
<li>激光笔</li>
|
|
|
|
<li>千层饼</li>
|
2018-10-10 22:03:03 +00:00
|
|
|
</ul>
|
2019-12-13 05:47:57 +00:00
|
|
|
<p>猫咪最讨厌的三件东西:</p>
|
2018-10-10 22:03:03 +00:00
|
|
|
<ol>
|
2019-12-13 05:47:57 +00:00
|
|
|
<li>跳蚤</li>
|
|
|
|
<li>打雷</li>
|
|
|
|
<li>同类</li>
|
2018-10-10 22:03:03 +00:00
|
|
|
</ol>
|
|
|
|
</div>
|
2019-12-13 05:47:57 +00:00
|
|
|
|
2020-07-15 09:55:06 +00:00
|
|
|
<form action="https://freecatphotoapp.com/submit-cat-photo">
|
2019-12-13 05:47:57 +00:00
|
|
|
<label><input type="radio" name="indoor-outdoor">室内</label>
|
|
|
|
<label><input type="radio" name="indoor-outdoor">室外</label><br>
|
|
|
|
<label><input type="checkbox" name="personality">忠诚</label>
|
|
|
|
<label><input type="checkbox" name="personality">懒惰</label>
|
|
|
|
<label><input type="checkbox" name="personality">积极</label><br>
|
|
|
|
<input type="text" placeholder="猫咪图片地址" required>
|
|
|
|
<button type="submit">提交</button>
|
2018-10-10 22:03:03 +00:00
|
|
|
</form>
|
|
|
|
</main>
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2019-12-13 05:47:57 +00:00
|
|
|
```html
|
2018-10-10 22:03:03 +00:00
|
|
|
// solution required
|
|
|
|
```
|
2020-02-17 16:40:55 +00:00
|
|
|
|
2018-10-10 22:03:03 +00:00
|
|
|
</section>
|
2019-12-13 05:47:57 +00:00
|
|
|
|