2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
id: 587d7fa7367417b2b2512bc6
|
2020-12-16 07:37:30 +00:00
|
|
|
title: 给元素添加内联样式
|
2018-10-10 22:03:03 +00:00
|
|
|
challengeType: 6
|
2020-09-17 16:13:05 +00:00
|
|
|
forumTopicId: 301475
|
2021-01-13 02:31:00 +00:00
|
|
|
dashedName: add-inline-styling-to-elements
|
2018-10-10 22:03:03 +00:00
|
|
|
---
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --description--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
D3 可以使用 `style()` 方法为动态元素添加内联 CSS 样式表。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
`style()` 方法以用逗号分隔的键值对作为参数。这里是一个将选中文本的颜色设为蓝色的例子:
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
`selection.style("color","blue");`
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --instructions--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
在编辑器中添加 `style()` 方法,使所有显示的文本的 `font-family` 为 `verdana`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
你的 `h2` 元素的 `font-family` 应该为 verdana。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert($('h2').css('font-family') == 'verdana');
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
你应该使用 `style()` 方法。
|
2020-09-17 16:13:05 +00:00
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
```js
|
|
|
|
assert(code.match(/\.style/g));
|
2018-10-10 22:03:03 +00:00
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```html
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
|
|
|
|
d3.select("body").selectAll("h2")
|
|
|
|
.data(dataset)
|
|
|
|
.enter()
|
|
|
|
.append("h2")
|
|
|
|
.text((d) => (d + " USD"))
|
|
|
|
// Add your code below this line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add your code above this line
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
```
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
```html
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
const dataset = [12, 31, 22, 17, 25, 18, 29, 14, 9];
|
|
|
|
|
|
|
|
d3.select("body").selectAll("h2")
|
|
|
|
.data(dataset)
|
|
|
|
.enter()
|
|
|
|
.append("h2")
|
|
|
|
.text((d) => (d + " USD"))
|
|
|
|
.style("font-family", "verdana")
|
|
|
|
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
```
|