2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aecf08806
|
|
|
|
|
challengeType: 0
|
2019-12-13 13:47:57 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/c2MvDtV'
|
|
|
|
|
forumTopicId: 18337
|
2020-01-08 12:40:17 +08:00
|
|
|
|
localeTitle: 使用 class 选择器设置单个元素的样式
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
CSS 的<code>class</code>具有可重用性,可应用于各种 HTML 元素。
|
|
|
|
|
一个 CSS<code>class</code>声明示例,如下所示:
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
.blue-text {
|
|
|
|
|
color: blue;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
可以看到,我们在<code><style></code>样式声明区域里,创建了一个名为<code>blue-text</code>的<code>class</code>选择器。
|
|
|
|
|
你可以将 CSS<code>class</code>选择器应用到一个HTML元素里,如下所示:
|
|
|
|
|
<code><h2 class="blue-text">CatPhotoApp</h2></code>
|
|
|
|
|
注意:在<code>style</code>样式区域声明里,<code>class</code>需以<code>.</code>开头。而在 HTML 元素里,<code>class</code>属性的前面不能添加<code>.</code>。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
在<code>style</code>样式声明里,把<code>h2</code>元素选择器改为<code>.red-text</code>class 选择器,同时将颜色<code>blue</code>变为<code>red</code>。
|
|
|
|
|
在<code>h2</code>元素里,添加一个<code>class</code>属性,且值为<code>'red-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>h2</code>元素应该为红色。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($("h2").css("color") === "rgb(255, 0, 0)");
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '<code>h2</code>元素应含有<code>red-text</code> class 选择器。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($("h2").hasClass("red-text"));
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '<code>style</code>样式声明区域里应该包含一个<code>red-text</code> class 选择器,并且它的颜色应为红色。'
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/\.red-text\s*\{\s*color\s*:\s*red;\s*\}/g));
|
2019-12-13 13:47:57 +08:00
|
|
|
|
- text: '不要在<code>h2</code>元素里使用行内样式:<code>style="color: red"</code>。'
|
2020-01-08 12:40:17 +08:00
|
|
|
|
testString: assert($("h2").attr("style") === undefined);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='html-seed'>
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
h2 {
|
|
|
|
|
color: blue;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<h2>CatPhotoApp</h2>
|
|
|
|
|
<main>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<p class="red-text">点击查看更多<a href="#">猫图</a>.</p>
|
|
|
|
|
|
|
|
|
|
<a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="一只仰卧着的萌猫"></a>
|
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
<div>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<p>猫咪最喜欢的三件东西:</p>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
<ul>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<li>猫薄荷</li>
|
|
|
|
|
<li>激光笔</li>
|
|
|
|
|
<li>千层饼</li>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</ul>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<p>猫咪最讨厌的三件东西:</p>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
<ol>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
<li>跳蚤</li>
|
|
|
|
|
<li>打雷</li>
|
|
|
|
|
<li>同类</li>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</ol>
|
|
|
|
|
</div>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
|
2020-07-15 02:55:06 -07:00
|
|
|
|
<form action="https://freecatphotoapp.com/submit-cat-photo">
|
2019-12-13 13:47:57 +08: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 18:03:03 -04:00
|
|
|
|
</form>
|
|
|
|
|
</main>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</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
|
|
|
|
|
```
|
2020-02-18 01:40:55 +09:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|
2019-12-13 13:47:57 +08:00
|
|
|
|
|