如何为通过js创建的raphael元素注册事件?

时间:2014-03-25 03:06:37

标签: raphael

假设我有n个对象(比如矩形),当用户按下按钮时,其大小和位置是随机创建的。如何在Raphael中注册点击事件,以便我知道当用户点击其中一个矩形时点击了哪个矩形/对象?这里的技巧是在循环内部创建n个数字。我需要能够处理何时单击任何矩形以便我可以执行某些操作(例如,将点击的矩形的颜色更改为蓝色。)以下是创建矩形的代码:

for (var i=1;i<3;i++) {
  x = Math.floor((Math.random()*200)+1);
  y = Math.floor((Math.random()*200)+1);
  width = Math.floor((Math.random()*25)+1) + 25;
  height = Math.floor((Math.random()*100)+1) + 50;
  r1 = paper.rect(x,y,width,height).attr({fill:"red"});
}

1 个答案:

答案 0 :(得分:0)

我想我在另一个帖子中找到了它。这就是它的样子......你看,看,发布在这里,然后你找到答案:

我只需要在创建矩形后在循环中添加以下处理程序:

 r1.click(function() { this.attr({fill:'blue'}); });

感谢:http://jsfiddle.net/CHUrB/298/