jQuery 未捕获的 ReferenceError:e 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13326351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:30:47  来源:igfitidea点击:

Uncaught ReferenceError: e is not defined

jquery

提问by Bryce

I am trying to do this:

我正在尝试这样做:

$("#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);
});

and in the console I get this:

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

Uncaught ReferenceError: e is not defined

Uncaught ReferenceError: e is not defined

I don't understand... I thought eis supposed to be a variable that JavaScript has already set...help?

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

回答by 0x499602D2

Change

改变

$("#canvasDiv").mouseover(function() {

to

$("#canvasDiv").mouseover(function(e) {

True, the first parameter to the callback is pre-defined, but in order to use it you must alias it through an argument name. That's why we specify eas the argument. In fact, it's not required that the argument name is e. This will work too:

诚然,回调的第一个参数是预定义的,但为了使用它,您必须通过参数名称为其别名。这就是我们指定e为参数的原因。事实上,参数名称不是必需的e。这也将起作用:

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

});

But you must alias the event object through the name eventin the function callback.

但是您必须通过event函数回调中的名称为事件对象设置别名。

回答by milgner

You are defining a callback function that will be invoked when the mouseover event occurs. The framework will pass information about the event as the first argument to this function and it is common to name this argument 'e'. But you have to declare it in the function parameters, i.e.

您正在定义一个回调函数,该函数将在发生鼠标悬停事件时调用。框架将有关事件的信息作为该函数的第一个参数传递,并且通常将此参数命名为“e”。但是你必须在函数参数中声明它,即

$('foo').mouseover(function (e) {

回答by Anoop

             .....missing e ---- -\/
$("#canvasDiv").mouseover(function(e) {
    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);
});