Why is the variable "event" available even when it is not passed as a parameter?

I wonder why the following code works in some browsers? That is, even if the click() parameter is missing, but the event variable exists and the dosomething method dosomething called in the event trigger object?

 $(<selector>).click(function () { $(event.target).<dosomething> }); 
+5
javascript jquery
Oct 16 '15 at 9:36
source share
1 answer

Why is the "event" variable available even if it is not passed as a parameter?

It is not so reliable. For example, this code will not work in Firefox.

Microsoft used the global variable event . DOM2 defined it as an argument to the handler. Chrome decided to drop the MS-specific bone code and do both. Firefox did not.

Even in browsers that run this code, note that event will be a raw event, not a jQuery-enhanced one. This means that, for example, in IE8 you cannot call event.preventDefault , because IE8 does not provide this function. jQuery will be if you accept the argument because jQuery provides an event object with standard functions even in browsers that do not have these functions.

+15
Oct. 16 '15 at 9:38
source share



All Articles