d3.select by属性值 [英] d3.select by attribute value
问题描述
我是d3的新手。我有这样定义的东西:
I am new to d3. I have something defined like this:
node = node.enter().append("circle")
.attr('id', function(d){ return d.id; })
.attr("class", "node")
.on('mouseover', mouseover_node)
.on("click", nodeClick);
现在在函数nodeClick中我想访问一个带有特殊ID的节点我正在寻找可以使用的东西,像这样:
Now in function nodeClick I want to access a node (or circle) with a special id. I am looking for something that I could use like this:
for(var i=0;i<maxId;i++) {
d3.select(the node with id = i).do....
有人知道我怎么能这样做吗?
Does anybody know how I can do this?
推荐答案
您的问题是 id
s和 name
s 必须以字母开头 。所以修改你的代码以为每个 id
添加一个字符串,例如
Your problem is that id
s and name
s must begin with a letter. So modify your code to prepend a string to each id
, e.g.
.attr('id', function(d){ return 'name' + d.id; })
然后,可以使用 d3.select('#name'+ i)
选择给定的节点。从 D3选择的文档:
Then, you can select a given node by using d3.select( '#name' + i )
. From the docs on D3 selections:
...您可以通过标签(div),类(.awesome),唯一的
标识符(#foo),属性= red])或包含
(父子代)。
... you can select by tag ("div"), class (".awesome"), unique identifier ("#foo"), attribute ("[color=red]"), or containment ("parent child").
这篇关于d3.select by属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!