我是 d3 的新手。我有这样的定义:
node = node.enter().append("circle")
.attr('id', function(d){ return d.id; })
.attr("class", "node")
.on('mouseover', mouseover_node)
.on("click", nodeClick);
现在,在函数 nodeClick 中,我想访问具有特殊 id 的节点(或圆)。我正在寻找可以像这样使用的东西:
for(var i=0;i<maxId;i++) {
d3.select(the node with id = i).do....
有人知道我该怎么做吗?
你的问题是ids and names 必须以字母开头 https://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html。因此修改您的代码以在每个前面添加一个字符串id
, e.g.
.attr('id', function(d){ return 'name' + d.id; })
然后,您可以使用以下命令选择给定节点d3.select( '#name' + i )
. From the docs https://github.com/mbostock/d3/wiki/Selections关于 D3 选择:
...您可以通过标签(“div”)、类(“.awesome”)、唯一进行选择
标识符(“#foo”)、属性(“[color=red]”)或包含
(“父子”)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)