d3.js使用Class和Attr为文本添加颜色

问题描述 投票:0回答:1

我是D3的新手,但我正在研究一切。这是我在StackOverflow上的第二篇文章。

底部的代码显示在本页的“Attr()”部分:

http://www.tutorialsteacher.com/d3js/dom-manipulation-using-d3js

问题是它不会将文本颜色更改为红色。我今天下午读过几十篇文章,但没有任何作用。我知道我可以使用内联“样式”命令,但我想了解所有替代方案。

<!doctype html>
<html>
<head>
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <style>
        .error {
            color: 'red'
        }
    </style>
</head>
<body>
    <p>Error: This is dummy error.</p>
    <script>
        d3.select("p").attr("class","error");
    </script>
</body>
</html>
javascript class d3.js colors attributes
1个回答
0
投票

添加.classed('classname',true)

<!doctype html>
<html>
<head>
  <script src="https://d3js.org/d3.v4.min.js"></script>
  <style>
    .error {
      color: #ff0000;
    }
  </style>
</head>
<body>
<p>Error: This is dummy error.</p>
<script>
  // to add
  d3.select("p").classed("error", true);
</script>
</body>
</html>

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.