admin管理员组文章数量:1337125
In an arbitrary JavaScript function, I wish to determine the upstream event.
The event listener function did not pass the event do the current function. As far as I know, window.event
is not WC3 and not available.
function listener(e) { subfunc(e.target); } function subfunc( element) { // here I wish to know which if any event responsible for me being called var event = ?; switch( event.type ) { ... } }
How does function subfunc()
determine the current event?
(Apologize if question asked before - seems it must have been - but cannot track it down.)
In an arbitrary JavaScript function, I wish to determine the upstream event.
The event listener function did not pass the event do the current function. As far as I know, window.event
is not WC3 and not available.
function listener(e) { subfunc(e.target); } function subfunc( element) { // here I wish to know which if any event responsible for me being called var event = ?; switch( event.type ) { ... } }
How does function subfunc()
determine the current event?
(Apologize if question asked before - seems it must have been - but cannot track it down.)
Share Improve this question edited Jan 15, 2023 at 14:13 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 23, 2011 at 9:33 cc youngcc young 20.3k32 gold badges95 silver badges150 bronze badges 5-
2
Your only chance is to pass
e
tosubfunc
, not onlye.target
. Or you setwindow.event = e;
insidelistener
, but passing the event object seems to be a cleaner approach to me. – Felix Kling Commented Sep 23, 2011 at 9:36 -
@Felix - on setting
window.event
, seems dangerous to try to maintain that state space. – cc young Commented Sep 23, 2011 at 9:42 - @spender - wow! seems like that would be a pretty useful thing to know. – cc young Commented Sep 23, 2011 at 9:42
- @ccyoung: Agreed, that's why I said passing the event object is better :) – Felix Kling Commented Sep 23, 2011 at 9:43
-
@Felix - right! seems a choice between extra arg and
this
. playing with code psychology now. – cc young Commented Sep 23, 2011 at 9:54
2 Answers
Reset to default 3You can do:
function listener(e) { subfunc.call(this, e); }
function subfunc( e ) {
var element = this; //<- 'this' points to your element, 'e.type' is event type
switch( e.type ) { ... }
}
Also other way:
function listener(e) { subfunc.call(e, e.target); }
function subfunc( element ) {
var event = this; //<- 'this' points to your event object
switch( event.type ) { ... }
}
That's not possible. There's only one property which can be accessed through another, reliable way: event.target
= this
(Provided that the function is called from within the scope of the event listener).
The event object should be passed to subfunc
.
本文标签: In JavaScripthow do you determine the current event if not passed as argStack Overflow
版权声明:本文标题:In JavaScript, how do you determine the current event if not passed as arg? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742303110a2449384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论