0
說我有n個對象(比如矩形),當用戶按下按鈕時,它們的大小和位置是隨機創建的。如何在Raphael中註冊點擊事件,以便在用戶單擊其中一個矩形時知道點擊了哪個矩形/對象?這裏的技巧是在循環內部創建n個數字。我需要能夠處理任何矩形點擊時,以便我可以執行一些操作(例如,將點擊矩形的顏色更改爲藍色。)下面是創建矩形的代碼:如何註冊通過js創建的raphael元素的事件?
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"});
}