为什么RaphaelJS中的这个事件处理程序会直接运行?

Why does this event handler in RaphaelJS, run straight away?

本文关键字:运行 程序 事件处理 RaphaelJS 为什么      更新时间:2023-09-26

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

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));
});  

.mousedown()需要一个函数引用作为它的参数。相反,您正在调用一个函数,并且所有.mousedown()作为其参数获得的都是undefined。因为需要将circle传递给enlarge(),所以不能直接传递对enlarge的引用。相反,将对enlarge()的调用包装在一个函数中:

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

因为你直接调用

button.mousedown(enlarge(circle));替换为以下代码:

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