使用jQuery的函数中的事件

Sot*_*ris 3 javascript jquery javascript-events

我知道如何以经典方式使用jQuery事件,例如:

$("#moveArea").mousemove(function(event){
    $("#info").empty().append("pageX is: "+event.pageX);
});
Run Code Online (Sandbox Code Playgroud)

演示:http://jsfiddle.net/Ey7kP/

我的问题是如何在我已经创建的函数中传递事件.我需要类似下面的内容,但我不知道如何实现这一目标

function cursorPos(event) {
  $("#info").empty().append("pageX is: "+event.pageX);      
}

$("#moveArea").mousemove(cursorPos(event));
Run Code Online (Sandbox Code Playgroud)

Ale*_*pin 8

做就是了

$("#moveArea").mousemove(cursorPos);
Run Code Online (Sandbox Code Playgroud)

既然你是在引用函数而不是调用它,那么就不需要传递参数了.jQuery会为你调用它并传递event给它.