Svg D3.js 如何使用条件来选择全部

Svg D3.js How to selectAll using condition

假设页面正文中有很多组(<g/>)元素。这些组元素中的一些具有 id,其中一些没有,但是只对具有以字符串 foo 开头的 id 的那些组元素感兴趣。

如何只对选定元素的子集进行操作?

需要这样的东西:

d3.selectAll("g").where(id like 'foo%')

[] selects attributes 和 ^= 意味着开始于所以你结束于此...

d3.selectAll("g[id^=foo]")

除了使用选择器做过滤,还可以使用D3的.filter()函数:

d3.selectAll("g").filter(function(d) { return this.id.match(/foo/).length > 0; });