像这样的 DOM:
<g.module>
<g.control>
<rect>
我没有找到最接近的 API: https://github.com/mbostock/d3/wiki/API-Reference
如何从它的父元素中获取最近的匹配元素?就像这样:
var module = d3.select(".control").closest(".module");
最佳答案
浏览器现在有 closest method on DOM node :
d3.select(rect.node().closest('svg'));
以及使用此方法与@JayB 类似的代码:
d3.selection.prototype.closest = function(selector) {
var arr = [];
this.each(function() {
arr.push(this.closest(selector));
});
return d3.selectAll(arr);
};
只允许使用:rect.closest('svg');
唯一的问题是IE不支持
关于javascript - d3有没有类似jQuery.closest(selector)的api?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375761/