2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: bad87fee1348bd9aedf08719
|
2020-12-16 00:37:30 -07:00
|
|
|
|
title: 使用缩写的十六进制编码
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 0
|
2019-12-13 13:47:57 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cRkpKAm'
|
|
|
|
|
forumTopicId: 18338
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: use-abbreviated-hex-code
|
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
|
|
|
|
许多人对超过 1600 万种颜色感到不知所措,并且很难记住十六进制编码。幸运的是,它也提供缩写的方法。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
例如,红色的 `#FF0000` 十六进制编码可以缩写成 `#F00`。在这种缩写形式里,三个数字分别代表着红(R)、绿(G)、蓝(B)三原色。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
这样,颜色的数量减少到了大约 4000 种。且在浏览器里 `#FF0000` 和 `#F00` 是完全相同的颜色。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
# --instructions--
|
|
|
|
|
|
2019-12-13 13:47:57 +08:00
|
|
|
|
接下来,使用缩写的十六进制编码给元素设置正确的颜色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
<table class='table table-striped'><tbody><tr><th>Color</th><th>Short Hex Code</th></tr><tr><td>Cyan</td><td><code>#0FF</code></td></tr><tr><td>Green</td><td><code>#0F0</code></td></tr><tr><td>Red</td><td><code>#F00</code></td></tr><tr><td>Fuchsia</td><td><code>#F0F</code></td></tr></tbody></table>
|
|
|
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
文本内容为 `I am red!` 的 `h1` 元素的字体颜色应该为红色。
|
2020-12-16 00:37:30 -07:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
assert($('.red-text').css('color') === 'rgb(255, 0, 0)');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
应使用红色的十六进制编码的缩写形式,不应使用 `#FF0000`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.red-text\s*?{\s*?color:\s*?#F00\s*?;\s*?}/gi));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
文本内容为 `I am green!` 的 `h1` 元素的字体颜色应该为绿色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.green-text').css('color') === 'rgb(0, 255, 0)');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
应使用绿色的十六进制编码的缩写形式,不应使用 `#00FF00`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.green-text\s*?{\s*?color:\s*?#0F0\s*?;\s*?}/gi));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
文本内容为 `I am cyan!` 的 `h1` 元素的字体颜色应该为蓝绿色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.cyan-text').css('color') === 'rgb(0, 255, 255)');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
应使用蓝绿色的十六进制编码的缩写形式,不应使用 `#00FFFF`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.cyan-text\s*?{\s*?color:\s*?#0FF\s*?;\s*?}/gi));
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
文本内容为 `I am fuchsia!` 的 `h1` 元素的字体颜色应该为紫红色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.fuchsia-text').css('color') === 'rgb(255, 0, 255)');
|
|
|
|
|
```
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-01-08 11:20:48 -08:00
|
|
|
|
应使用紫红色的十六进制编码的缩写形式,不应使用 `#FF00FF`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/\.fuchsia-text\s*?{\s*?color:\s*?#F0F\s*?;\s*?}/gi));
|
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>
|
|
|
|
|
.red-text {
|
|
|
|
|
color: #000000;
|
|
|
|
|
}
|
|
|
|
|
.fuchsia-text {
|
|
|
|
|
color: #000000;
|
|
|
|
|
}
|
|
|
|
|
.cyan-text {
|
|
|
|
|
color: #000000;
|
|
|
|
|
}
|
|
|
|
|
.green-text {
|
|
|
|
|
color: #000000;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<h1 class="red-text">I am red!</h1>
|
|
|
|
|
|
|
|
|
|
<h1 class="fuchsia-text">I am fuchsia!</h1>
|
|
|
|
|
|
|
|
|
|
<h1 class="cyan-text">I am cyan!</h1>
|
|
|
|
|
|
|
|
|
|
<h1 class="green-text">I am green!</h1>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```html
|
|
|
|
|
<style>
|
|
|
|
|
.red-text {
|
|
|
|
|
color: #F00;
|
|
|
|
|
}
|
|
|
|
|
.fuchsia-text {
|
|
|
|
|
color: #F0F;
|
|
|
|
|
}
|
|
|
|
|
.cyan-text {
|
|
|
|
|
color: #0FF;
|
|
|
|
|
}
|
|
|
|
|
.green-text {
|
|
|
|
|
color: #0F0;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<h1 class="red-text">I am red!</h1>
|
|
|
|
|
|
|
|
|
|
<h1 class="fuchsia-text">I am fuchsia!</h1>
|
|
|
|
|
|
|
|
|
|
<h1 class="cyan-text">I am cyan!</h1>
|
|
|
|
|
|
|
|
|
|
<h1 class="green-text">I am green!</h1>
|
|
|
|
|
```
|