未捕获的ReferenceError: e没有定义

时间:2021-12-16 19:00:42

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

未捕获的ReferenceError: e没有定义

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

我不明白……我认为e应该是JavaScript已经设置的变量。

3 个解决方案

#1


10  

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 e as 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 event in the function callback.

但是您必须在函数回调中通过名称事件将事件对象混叠。

#2


2  

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.

您正在定义一个回调函数,当mouseover事件发生时将调用它。框架将把有关事件的信息作为第一个参数传递给这个函数,通常将这个参数命名为“e”。但是你必须在函数参数中声明它,也就是。

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

#3


1  

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

#1


10  

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 e as 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 event in the function callback.

但是您必须在函数回调中通过名称事件将事件对象混叠。

#2


2  

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.

您正在定义一个回调函数,当mouseover事件发生时将调用它。框架将把有关事件的信息作为第一个参数传递给这个函数,通常将这个参数命名为“e”。但是你必须在函数参数中声明它,也就是。

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

#3


1  

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