---
id: 587d7fa7367417b2b2512bc8
title: Add Classes with D3
required:
- src: 'https://cdnjs.cloudflare.com/ajax/libs/d3/4.3.0/d3.min.js'
challengeType: 6
videoUrl: ''
localeTitle: 使用D3添加类
---
## Description
即使对于较小的应用程序,在HTML元素上使用大量内联样式也很难管理。使用CSS规则将类添加到元素和样式一次更容易。 D3具有attr()
方法,可以向元素添加任何HTML属性,包括类名。 attr()
方法的工作方式与style()
工作方式相同。它采用逗号分隔值,并可以使用回调函数。这是一个向选择中添加“容器”类的示例: selection.attr("class", "container");
## Instructions
将attr()
方法添加到编辑器中的代码中,并在div
元素上添加一个bar
类。
## Tests
```yml
tests:
- text: 你的div
元素应该有一类bar
。
testString: 'assert($("div").attr("class") == "bar", "Your div
elements should have a class of bar
.");'
- text: 您的代码应使用attr()
方法。
testString: 'assert(code.match(/\.attr/g), "Your code should use the attr()
method.");'
```
## Challenge Seed
## Solution
```js
// solution required
```