问题
I suspect that the answer lies somewhere between my ignorance of what kind of object I'm actually working and the inner darkness of D3 and/or SVG.
I can see 30 circles on the screen. So I go in the console like this.
var circles = d3.select("#svg1").selectAll("circle");
>undefined
circles;
>[Array[30]]...
circles.length;
>1
I know I'm targeting the right elements because executing .remove() on the set clears them from the screen. But what's up with the count?
回答1:
var circles = d3.select("#svg1").selectAll("circle");
circles is an array of array, hence the length is 1. That is the length of the outer array. If you want to get the count of circles selected then you should use d3s built-in method size.
var count = d3.select("#svg1").selectAll("circle").size();
This will give you the expected result.
来源:https://stackoverflow.com/questions/35101259/why-are-the-selectall-d3-created-circles-in-my-svg-said-to-be-1-element-using