未捕获的ReferenceError:未定义e
我想这样做:
$("#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); });
在控制台中我得到了这个:
Uncaught ReferenceError: e is not defined
我不明白……我认为e
应该是JavaScript已经设置的变量…帮助?
更改
$("#canvasDiv").mouseover(function() {
至
$("#canvasDiv").mouseover(function(e) {
是的,回调的第一个参数是预定义的,但为了使用它,您必须通过参数名称对其进行别名。 这就是我们将e
指定为参数的原因。 实际上,参数名称不需要e
。 这也可以:
$('#canvasDiv').mouseover(function( event ) { });
但是您必须通过函数回调中的name event
为事件对象添加别名。
您正在定义一个回调函数,该函数将在鼠标hover事件发生时调用。 框架将有关事件的信息作为此函数的第一个参数传递,并且通常将此参数命名为“e”。 但是你必须在函数参数中声明它,即
$('foo').mouseover(function (e) {
.....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); });