我创建了几个d3矩形,并给每个矩形一个单击事件:首先,我创建了容器:
var svgContainer = d3.select("#Container")
.append("svg")
.attr("width", 720)
.attr("height", 45);然后,在for-循环中,我在容器内创建每个矩形:
var rectangle = svgContainer.append("rect")
.attr("x", 10 + xAppend)
.attr("y", 5)
.attr("width", 120)
.attr("height", 35)
.attr("stroke", "black")
.attr("name", name)
.on("click", function () {
// Click event...
//...
});现在在另一个函数中,我想触发这个点击函数。变量“矩形”不是全局的,它是在for循环中创建的,我从for循环创建了6个矩形。
到目前为止,我在其他功能中所做的是:
var tempContainer = d3.select("#Container");
var test = tempContainer.selectAll("rect"); 这给了我一个由1个对象组成的数组,这个对象是一个由6个矩形组成的数组,然后我循环遍历这个数组来找到我想要的矩形。
for (var i = 0; i < test[0].length; i++) {
if (search.item.desc == test[0][i].attributes[5].value)
{
var testing = test[0][i];
//testing.attributes[5].value works fine
//testing.click(); does not work
//testing.on('click')(); does not work
}
}在for-循环中,变量‘the’是我想要的矩形,我可以像在上面的if语句中那样访问它的属性。但是我不能访问并触发function函数。
发布于 2015-07-27 11:23:57
可以使用以下方法访问on事件函数:
testing.__onclick();https://stackoverflow.com/questions/31651485
复制相似问题