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'並檢查捆綁的數據嗎? –