获取D3中元素的颜色

克里斯蒂娜

对于以下d3代码:

var previousElement = d3.select(this);
console.log(previousElement.attr("style"));

结果是这样的:

fill: red;

或者

fill: rgb(152, 223, 138);

然后,如何获得该元素的确切颜色,以便做出如下判断:

if ( elementColor == 'red')

或者

if (elementColor == 'rgb(152, 223, 138)')

我试过了var elementColor = previousElement.attr("style").fill,它回来了null

布兰特·奥尔森(Brant Olsen)

我相信您想要的previousElement.style('fill')不是使用.attr('style')有关文档,请参见https://github.com/mbostock/d3/wiki/Selections#style

您可能还希望查看D3 Colors文档,https://github.com/mbostock/d3/wiki/Colors,因为这可能对您有更多帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章