I'm novice with both JS and jQuery, and I'm a little bit confused about what situations would require you to pass event
as an argument into the function, and what situations you would not need to.
我是JS和jQuery的新手,我对于什么情况需要你将事件作为参数传递给函数以及你不需要的情况我有点困惑。
For example:
$(document).ready(function() {
$('#foo').click(function() {
// Do something
});
});
versus
$(document).ready(function() {
$('#foo').click(function(event) {
// Do something
});
});
3 个解决方案
#1
The event
argument has a few uses. You only need to specify it as an argument to your handler if you're actually going to make use of it -- JavaScript handles variable numbers of arguments without complaint.
事件参数有一些用途。如果你真的要使用它,你只需要将它指定为你的处理程序的参数 - JavaScript处理可变数量的参数而不会抱怨。
The most common use you'll see is to prevent the default behavior of the action that triggered the event. So:
您将看到的最常见用途是防止触发事件的操作的默认行为。所以:
$('a.fake').click(function(e) {
e.preventDefault();
alert("This is a fake link!");
});
...would stop any links with the class fake
from actually going to their href
when clicked. Likewise, you can cancel form submissions with it, e.g. in validation methods. This is like return false
, but rather more reliable.
...当点击时,会停止与类假的任何链接实际转到他们的href。同样,您可以使用它取消表单提交,例如在验证方法中。这就像返回false,但更可靠。
jQuery's event
object is actually a cross-browser version of the standard event
argument provided in everything but IE. It's essentially a shortcut, that lets you use only one code path instead of having to check what browser you're using in every event handler.
jQuery的事件对象实际上是除IE之外的所有内容中提供的标准事件参数的跨浏览器版本。它本质上是一种快捷方式,它允许您只使用一个代码路径,而不必检查您在每个事件处理程序中使用的浏览器。
(If you read non-jQuery code you'll see a lot of the following, which is done to work around IE's deficiency.
(如果你阅读非jQuery代码,你会看到很多以下内容,这是为了解决IE的不足之处。
function(e) {
e = e || window.event; // For IE
It's a pain, and libraries make it so much easier to deal with.)
这是一个痛苦,图书馆使它更容易处理。)
There's a full accounting of its properties in the jQuery docs. Essentially, include it if you see anything you need there, and don't worry otherwise. I like to include it always, just so I never have to remember to add it in later if I decide that it's needed after all.
在jQuery文档中对其属性进行了完整的计算。基本上,如果你在那里看到任何你需要的东西,请加入它,不要担心。我总是喜欢把它包括在内,所以如果我认为毕竟需要的话,我就不必记得在以后添加它。
#2
You only need the event if you're going to use it in the body of the handler.
如果您要在处理程序的主体中使用它,则只需要该事件。
#3
Since you are using jQuery, you only put event
as an argument if you need to use the event in the handler, such as if you need the key that was pressed on a keypress
event.
由于您使用的是jQuery,因此如果需要在处理程序中使用事件,则只将事件作为参数,例如,如果您需要在按键事件上按下的键。
In JS, without jQuery or Prototype etc., you need to pass the event
as a parameter for standards compliant browsers like Firefox, but the event is not passed as an argument in IE. IE actually maintains a global variable window.event
. So in your handler (sans library) you need to check if the argument is undefined
; if so, grab the global variable.
在JS中,没有jQuery或Prototype等,您需要将事件作为标准兼容浏览器(如Firefox)的参数传递,但该事件不会作为IE中的参数传递。 IE实际上维护着一个全局变量window.event。所以在你的处理程序(sans库)中,你需要检查参数是否未定义;如果是这样,抓住全局变量。
function eventHandler(evt) {
var theEvent = evt || window.event;
//use the event
}
But a library like jQuery takes care of that for you.
但像jQuery这样的库可以为你解决这个问题。
I honestly don't recommend using a library until you have learned the language. But if this is for a job, the by all means use the library, but learn the details of JS on your own, so you can better appreciate it.
老实说,在你学会了这门语言之前,我不建议你使用图书馆。但如果这是一份工作,那么一定要使用图书馆,但要自己学习JS的细节,这样你就可以更好地欣赏它了。
#1
The event
argument has a few uses. You only need to specify it as an argument to your handler if you're actually going to make use of it -- JavaScript handles variable numbers of arguments without complaint.
事件参数有一些用途。如果你真的要使用它,你只需要将它指定为你的处理程序的参数 - JavaScript处理可变数量的参数而不会抱怨。
The most common use you'll see is to prevent the default behavior of the action that triggered the event. So:
您将看到的最常见用途是防止触发事件的操作的默认行为。所以:
$('a.fake').click(function(e) {
e.preventDefault();
alert("This is a fake link!");
});
...would stop any links with the class fake
from actually going to their href
when clicked. Likewise, you can cancel form submissions with it, e.g. in validation methods. This is like return false
, but rather more reliable.
...当点击时,会停止与类假的任何链接实际转到他们的href。同样,您可以使用它取消表单提交,例如在验证方法中。这就像返回false,但更可靠。
jQuery's event
object is actually a cross-browser version of the standard event
argument provided in everything but IE. It's essentially a shortcut, that lets you use only one code path instead of having to check what browser you're using in every event handler.
jQuery的事件对象实际上是除IE之外的所有内容中提供的标准事件参数的跨浏览器版本。它本质上是一种快捷方式,它允许您只使用一个代码路径,而不必检查您在每个事件处理程序中使用的浏览器。
(If you read non-jQuery code you'll see a lot of the following, which is done to work around IE's deficiency.
(如果你阅读非jQuery代码,你会看到很多以下内容,这是为了解决IE的不足之处。
function(e) {
e = e || window.event; // For IE
It's a pain, and libraries make it so much easier to deal with.)
这是一个痛苦,图书馆使它更容易处理。)
There's a full accounting of its properties in the jQuery docs. Essentially, include it if you see anything you need there, and don't worry otherwise. I like to include it always, just so I never have to remember to add it in later if I decide that it's needed after all.
在jQuery文档中对其属性进行了完整的计算。基本上,如果你在那里看到任何你需要的东西,请加入它,不要担心。我总是喜欢把它包括在内,所以如果我认为毕竟需要的话,我就不必记得在以后添加它。
#2
You only need the event if you're going to use it in the body of the handler.
如果您要在处理程序的主体中使用它,则只需要该事件。
#3
Since you are using jQuery, you only put event
as an argument if you need to use the event in the handler, such as if you need the key that was pressed on a keypress
event.
由于您使用的是jQuery,因此如果需要在处理程序中使用事件,则只将事件作为参数,例如,如果您需要在按键事件上按下的键。
In JS, without jQuery or Prototype etc., you need to pass the event
as a parameter for standards compliant browsers like Firefox, but the event is not passed as an argument in IE. IE actually maintains a global variable window.event
. So in your handler (sans library) you need to check if the argument is undefined
; if so, grab the global variable.
在JS中,没有jQuery或Prototype等,您需要将事件作为标准兼容浏览器(如Firefox)的参数传递,但该事件不会作为IE中的参数传递。 IE实际上维护着一个全局变量window.event。所以在你的处理程序(sans库)中,你需要检查参数是否未定义;如果是这样,抓住全局变量。
function eventHandler(evt) {
var theEvent = evt || window.event;
//use the event
}
But a library like jQuery takes care of that for you.
但像jQuery这样的库可以为你解决这个问题。
I honestly don't recommend using a library until you have learned the language. But if this is for a job, the by all means use the library, but learn the details of JS on your own, so you can better appreciate it.
老实说,在你学会了这门语言之前,我不建议你使用图书馆。但如果这是一份工作,那么一定要使用图书馆,但要自己学习JS的细节,这样你就可以更好地欣赏它了。