2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7dbf367417b2b2512bba
|
2021-03-07 09:15:14 -07:00
|
|
|
|
title: 使用 @each 遍历列表中的项目
|
2018-10-10 18:03:03 -04:00
|
|
|
|
challengeType: 0
|
2020-09-07 16:11:08 +08:00
|
|
|
|
forumTopicId: 301461
|
2021-01-13 03:31:00 +01:00
|
|
|
|
dashedName: use-each-to-map-over-items-in-a-list
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --description--
|
2020-09-07 16:11:08 +08:00
|
|
|
|
|
2021-03-07 09:15:14 -07:00
|
|
|
|
上一个挑战显示了 `@for` 指令如何通过起始值和结束值循环一定次数。 Sass 还提供 `@each` 指令,该指令循环遍历列表或映射中的每个项目。 在每次迭代时,变量将从列表或映射分配给当前值。
|
2020-09-07 16:11:08 +08:00
|
|
|
|
|
|
|
|
|
```scss
|
|
|
|
|
@each $color in blue, red, green {
|
|
|
|
|
.#{$color}-text {color: $color;}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-09-18 11:22:53 -07:00
|
|
|
|
map 的语法略有不同。 这是一个示例:
|
2020-09-07 16:11:08 +08:00
|
|
|
|
|
|
|
|
|
```scss
|
|
|
|
|
$colors: (color1: blue, color2: red, color3: green);
|
|
|
|
|
|
|
|
|
|
@each $key, $color in $colors {
|
|
|
|
|
.#{$color}-text {color: $color;}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-07 09:15:14 -07:00
|
|
|
|
请注意,需要用 `$key` 变量来引用 map 中的键。 否则,编译后的 CSS 将包含 `color1`,`color2`...... 以上两个代码示例都转换为以下 CSS:
|
2020-09-07 16:11:08 +08:00
|
|
|
|
|
|
|
|
|
```scss
|
|
|
|
|
.blue-text {
|
|
|
|
|
color: blue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.red-text {
|
|
|
|
|
color: red;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.green-text {
|
|
|
|
|
color: green;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --instructions--
|
2020-09-07 16:11:08 +08:00
|
|
|
|
|
2021-03-07 09:15:14 -07:00
|
|
|
|
编写一个 `@each` 指令遍历列表:`blue, black, red` ,将每个变量分配给 class 为`.color-bg` 的项目,使每个项目的 `color` 都不一样。 每个 class 都应该将 `background-color` 设置为相应的颜色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2021-03-07 09:15:14 -07:00
|
|
|
|
代码应使用 `@each` 指令。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert(code.match(/@each /g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-07 09:15:14 -07:00
|
|
|
|
`.blue-bg` class 的 `background-color` 应为蓝色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.blue-bg').css('background-color') == 'rgb(0, 0, 255)');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
|
|
|
|
|
2021-03-07 09:15:14 -07:00
|
|
|
|
`.black-bg` class 的 `background-color` 应为黑色。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.black-bg').css('background-color') == 'rgb(0, 0, 0)');
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
|
2021-03-07 09:15:14 -07:00
|
|
|
|
`.red-bg` class 的 `background-color` 应为红色。
|
2020-09-07 16:11:08 +08:00
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
```js
|
|
|
|
|
assert($('.red-bg').css('background-color') == 'rgb(255, 0, 0)');
|
2020-09-07 16:11:08 +08:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style type='text/scss'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
div {
|
|
|
|
|
height: 200px;
|
|
|
|
|
width: 200px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div class="blue-bg"></div>
|
|
|
|
|
<div class="black-bg"></div>
|
|
|
|
|
<div class="red-bg"></div>
|
|
|
|
|
```
|
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
|
```html
|
|
|
|
|
<style type='text/scss'>
|
|
|
|
|
|
|
|
|
|
@each $color in blue, black, red {
|
|
|
|
|
.#{$color}-bg {background-color: $color;}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
div {
|
|
|
|
|
height: 200px;
|
|
|
|
|
width: 200px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div class="blue-bg"></div>
|
|
|
|
|
<div class="black-bg"></div>
|
|
|
|
|
<div class="red-bg"></div>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
```html
|
|
|
|
|
<style type='text/scss'>
|
|
|
|
|
|
|
|
|
|
$colors: (color1: blue, color2: black, color3: red);
|
|
|
|
|
|
|
|
|
|
@each $key, $color in $colors {
|
|
|
|
|
.#{$color}-bg {background-color: $color;}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
div {
|
|
|
|
|
height: 200px;
|
|
|
|
|
width: 200px;
|
|
|
|
|
}
|
|
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<div class="blue-bg"></div>
|
|
|
|
|
<div class="black-bg"></div>
|
|
|
|
|
<div class="red-bg"></div>
|
|
|
|
|
```
|