是否有一个选择查询来抓取除当前所选内容之外的所有内容?
问题描述:
在我的d3js应用程序中,当用户将鼠标悬停在特定的圆上时,我将其放大。这没有问题。同时,我想选择“所有其他”,并使它们变小。什么是抓住“所有其他圈子”的好询问?是否有一个选择查询来抓取除当前所选内容之外的所有内容?
答
根据您的需要,您可以使用selection.filter或常用的selection.select的鲜为人知的功能形式。 http://jsfiddle.net/9TmXs/
.on('click', function (d) {
// The clicked element returns to its original size
d3.select(this).transition() // ...
var circles = d3.selectAll('svg circle');
// All other elements resize randomly.
circles.filter(function (x) { return d.id != x.id; })
.transition() // ...
});
另一种通用方法,比较DOM元素本身:
如果您在使用key functions,这是推荐的方式绑定你的DOM元素的数据,那么你可以选择的主要筛选http://jsfiddle.net/FDt8S/
.on('click', function (d) {
// The clicked element returns to its original size
d3.select(this).transition() // ..
var self = this;
var circles = d3.selectAll('svg circle');
// All other elements resize randomly.
circles.filter(function (x) { return self != this; })
.transition()
// ...
});