Javascript jQuery:如何在事件处理函数中获取事件对象而不将其作为参数传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5849370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: How to get the event object in an event handler function without passing it as an argument?
提问by LazNiko
I have an onclick
attribute on my link:
我的onclick
链接上有一个属性:
<a href="#" onclick="myFunc(1,2,3)">click</a>
That points to this event handler in JavaScript:
这指向 JavaScript 中的这个事件处理程序:
function myFunc(p1,p2,p3) {
//need to refer to the current event object:
alert(evt.type);
}
Since the event object "evt" is not passed to a parameter, is it still possible to obtain this object?
既然事件对象“evt”没有传递给参数,那么还有可能获取到这个对象吗?
I tried window.event
and $(window.event)
, but both are undefined
.
我试过window.event
和$(window.event)
,但都是undefined
。
Any idea?
任何的想法?
回答by T.J. Crowder
Since the event object "evt" is not passed from the parameter, is it still possible to obtain this object?
既然没有从参数中传递事件对象“evt”,那么还有可能获取到这个对象吗?
No, not reliably. IE and some other browsers make it available as window.event
(not $(window.event)
), but that's non-standard and not supported by all browsers (famously, Firefox does not).
不,不可靠。IE 和其他一些浏览器将其作为window.event
(not $(window.event)
) 提供,但这是非标准的,并非所有浏览器都支持(众所周知,Firefox 不支持)。
You're better off passing the event object into the function:
您最好将事件对象传递给函数:
<a href="#" onclick="myFunc(event, 1,2,3)">click</a>
That works even on non-IE browsers because they execute the code in a context that has an event
variable (and works on IE because event
resolves to window.event
). I've tried it in IE6+, Firefox, Chrome, Safari, and Opera. Example: http://jsbin.com/iwifu4
这甚至适用于非 IE 浏览器,因为它们在具有event
变量的上下文中执行代码(并且适用于 IE,因为event
解析为window.event
)。我已经在 IE6+、Firefox、Chrome、Safari 和 Opera 中尝试过。示例:http: //jsbin.com/iwifu4
But your bestbet is to use modern event handling:
但最好的办法是使用现代事件处理:
HTML:
HTML:
<a href="#">click</a>
JavaScript using jQuery (since you're using jQuery):
使用 jQuery 的 JavaScript(因为您使用的是 jQuery):
$("selector_for_the_anchor").click(function(event) {
// Call `myFunc`
myFunc(1, 2, 3);
// Use `event` here at the event handler level, for instance
event.stopPropagation();
});
...or if you really want to pass event
into myFunc
:
...或者如果你真的想event
进入myFunc
:
$("selector_for_the_anchor").click(function(event) {
myFunc(event, 1, 2, 3);
});
The selector can be anything that identifies the anchor. You have a very rich setto choose from (nearly all of CSS3, plus some). You could add an id
or class
to the anchor, but again, you have other choices. If you can use where it is in the document rather than adding something artificial, great.
选择器可以是任何标识锚点的东西。你有一个非常丰富的集合可供选择(几乎所有的 CSS3,加上一些)。您可以向锚点添加id
或class
,但同样,您还有其他选择。如果您可以使用它在文档中的位置而不是添加一些人为的东西,那就太好了。
回答by otakustay
in IE you can get the event object by window.event
in other browsers with no 'use strict'
directive, it is possible to get by arguments.callee.caller.arguments[0]
.
在 IE 中,您可以通过window.event
在其他浏览器中不带'use strict'
指令的方式获取事件对象,可以通过arguments.callee.caller.arguments[0]
.
function myFunc(p1, p2, p3) {
var evt = window.event || arguments.callee.caller.arguments[0];
}
回答by Pointy
Write your event handler declaration like this:
像这样编写您的事件处理程序声明:
<a href="#" onclick="myFunc(event,1,2,3)">click</a>
Then your "myFunc()" function can access the event.
然后您的“myFunc()”函数可以访问该事件。
The string value of the "onclick" attribute is converted to a function in a way that's almost exactly the same as the browser (internally) calling the Function constructor:
“onclick”属性的字符串值转换为函数的方式与浏览器(内部)调用函数构造函数几乎完全相同:
theAnchor.onclick = new Function("event", theOnclickString);
(except in IE). However, because "event" is a global in IE (it's a window attribute), you'll be able to pass it to the function that way in any browser.
(IE 除外)。但是,因为“事件”在 IE 中是一个全局变量(它是一个窗口属性),所以您可以在任何浏览器中以这种方式将其传递给函数。
回答by Edgar Villegas Alvarado
If you call your event handler on markup, as you're doing now, you can't (x-browser). But if you bind the click event with jquery, it's possible the following way:
如果你在标记上调用你的事件处理程序,就像你现在所做的那样,你不能 (x-browser)。但是如果你用jquery绑定click事件,可以通过以下方式:
Markup:
标记:
<a href="#" id="link1" >click</a>
Javascript:
Javascript:
$(document).ready(function(){
$("#link1").click(clickWithEvent); //Bind the click event to the link
});
function clickWithEvent(evt){
myFunc('p1', 'p2', 'p3');
function myFunc(p1,p2,p3){ //Defined as local function, but has access to evt
alert(evt.type);
}
}
Since the event ob
自从事件ob