javascript 在 d3 中设置 ID 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13188395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:01:27  来源:igfitidea点击:

Setting an id issue in d3

javascriptcastingd3.js

提问by user1782677

So this is what I'm doing:

所以这就是我正在做的:

selection = canvas.selectAll("circle").data(mydata)

            selection.enter().append("circle")

            selection
                .attr("id", function(d, i){ var result = i+''; return result; })

In short, I'm trying to set the id of each element to be a string representation of its index.

简而言之,我试图将每个元素的 id 设置为其索引的字符串表示形式。

However, when I try to access that particular element later by using d3.select("#1")or by using a number variable i and going d3.select("#" + i.toString()), nothing seems to work.

但是,当我稍后尝试通过使用d3.select("#1")或使用数字变量 i 和 going访问该特定元素时d3.select("#" + i.toString()),似乎没有任何效果。

What am I doing wrong?

我究竟做错了什么?

回答by user1782677

I figured it out, apparently ids can't start with a number.

我想通了,显然 ids 不能以数字开头。