0
当用户点击legend
我隐藏并显示path
。有用。但是,我想保持legend
半透明,当path
被隐藏。d3js - 如何使用`this`关键字或替代项是什么?
为此,我试图使用this
关键字将样式添加到legend
,但它不起作用。
这样做的正确方法是什么?她是我的代码:
legends.on('click', function(d, i) {
var path = d3.select("#" + d.name);
var visibility = path.style('opacity');
path.style("opacity", visibility == 1 ? 0 : 1);
if (!visibility) {
d3.select(this).style('opacity', 0.5);//not working!
this.style('opacity', 0.5);//not working!
}
});
更新
试过仍然这样不工作:
legends.forEach(function(group) {
var legend = group;
legend.on('click', function(d, i) { //throws error
var path = d3.select("#" + d.name);
var visibility = path.style('opacity');
path.style("opacity", visibility == 1 ? 0 : 1);
console.log(legend);
});
})
更新
你能发布的jsfiddle/JSBin/...出的问题? –
由于场景需要多个依赖项,所以这不太容易在小提琴中重现 – 3gwebtrain
您可以通过控制台输出'd'并检查捆绑的数据吗? –