2011-11-21 43 views
2

为什么RaphaelJS(2.0.0)中的这个事件处理程序直接运行,而不是当您点击它时?为什么RaphaelJS中的这个事件处理程序直接运行?

function enlarge (shape) { 
    shape.animate({'transform' : 's2'}, 200); 
} 

jQuery(function() { 
    var paper = Raphael("canvas", 1000, 1000); 
    var button = paper.circle(300, 50, 20); 
    button.attr({'fill':'black'}); 

    var circle = paper.circle(50, 50, 20); 
    circle.attr({'fill':'red'}); 

    button.mousedown(enlarge(circle)); 
}); 
+1

据透露,如果更换'的jQuery(函数(){'和'的jQuery(函数($){'你可以使用'$'那个函数内不得不一直编写'jQuery'(即使在你的情况下,因为你没有使用它而没有关系) – ThiefMaster

回答

3

.mousedown()期望函数引用作为其参数。相反,你是一个函数调用,所有.mousedown()得到的参数是undefined。由于您需要通过circleenlarge(),因此无法直接传递对enlarge的引用。相反,包您来电enlarge()的功能:

button.mousedown(function() { 
    enlarge(circle); 
}); 
3

因为你电话它立竿见影。

与此代码替换button.mousedown(enlarge(circle));

button.mousedown(function() { 
    enlarge(circle) 
}); 
相关问题