具有多个事件处理程序传递参数的多个元素; d3.js;简化代码
Multiple elements with multiple event handlers passing parameters; d3.js; simplify code
如何动态生成这些事件处理程序?我想将其作为循环遍历 element.length.
的单个语句
d3.select("#wordcloud_0_im").on("click", function(d,i)
{
var x =1;
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
})
d3.select("#wordcloud_1_im").on("click", function(d,i)
{
var x =2;
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
})
d3.select("#wordcloud_2_im").on("click", function(d,i)
{
var x =3;
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
})
我不知道该怎么做。谢谢。
解决方案:
function handleElement(i) {
document.getElementById('wordcloud_' + i + '_im').onclick=function(d,e) {
wordcount = 40;
x=i;
console.log("x :" + x);
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
};
}
for(i=1; i<35; i++)
handleElement(i);
如何动态生成这些事件处理程序?我想将其作为循环遍历 element.length.
的单个语句 d3.select("#wordcloud_0_im").on("click", function(d,i)
{
var x =1;
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
})
d3.select("#wordcloud_1_im").on("click", function(d,i)
{
var x =2;
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
})
d3.select("#wordcloud_2_im").on("click", function(d,i)
{
var x =3;
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
})
我不知道该怎么做。谢谢。
解决方案:
function handleElement(i) {
document.getElementById('wordcloud_' + i + '_im').onclick=function(d,e) {
wordcount = 40;
x=i;
console.log("x :" + x);
wordclouds(keyfile,start=(x-1)*wordcount,stop=x*wordcount);
};
}
for(i=1; i<35; i++)
handleElement(i);