未捕获的ReferenceError:未定义e

Bry*_*yce 4 jquery

我想这样做:

$("#canvasDiv").mouseover(function() {
    var pageCoords = "( " + e.pageX + ", " + e.pageY + " )";
    var clientCoords = "( " + e.clientX + ", " + e.clientY + " )";
    $(".filler").text("( e.pageX, e.pageY ) : " + pageCoords);
    $(".filler").text("( e.clientX, e.clientY ) : " + clientCoords);
});
Run Code Online (Sandbox Code Playgroud)

在控制台中我得到了这个:

Uncaught ReferenceError: e is not defined

我不明白......我认为e应该是JavaScript已经设置的变量...帮助?

0x4*_*2D2 12

更改

$("#canvasDiv").mouseover(function() {
Run Code Online (Sandbox Code Playgroud)

$("#canvasDiv").mouseover(function(e) {
Run Code Online (Sandbox Code Playgroud)

是的,回调的第一个参数是预定义的,但为了使用它,您必须通过参数名称对其进行别名.这就是我们指定e为参数的原因.实际上,参数名称不是必需的e.这也可以:

$('#canvasDiv').mouseover(function( event ) {

});
Run Code Online (Sandbox Code Playgroud)

但是您必须通过event函数回调中的名称为事件对象添加别名.