最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing 'event' into the function as an argument - Stack Overflow

programmeradmin3浏览0评论

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.

For example:

      $(document).ready(function() {

        $('#foo').click(function() {
         // Do something
});

      });

versus

      $(document).ready(function() {

        $('#foo').click(function(event) {
         // Do something
});

      });

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.

For example:

      $(document).ready(function() {

        $('#foo').click(function() {
         // Do something
});

      });

versus

      $(document).ready(function() {

        $('#foo').click(function(event) {
         // Do something
});

      });
Share Improve this question asked Feb 1, 2009 at 5:26 MatthewMatthew 7,7357 gold badges41 silver badges39 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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 plaint.

The most mon 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.

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.

(If you read non-jQuery code you'll see a lot of the following, which is done to work around IE's deficiency.

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.

You only need the event if you're going to use it in the body of the handler.

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.

In JS, without jQuery or Prototype etc., you need to pass the event as a parameter for standards pliant 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.

function eventHandler(evt) {
  var theEvent = evt || window.event;
  //use the event
}

But a library like jQuery takes care of that for you.

I honestly don't remend 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.

发布评论

评论列表(0)

  1. 暂无评论