在 JavaScript 中,如果不作为 arg 传递,如何确定当前事件?

发布于 2024-12-06 15:33:38 字数 410 浏览 0 评论 0原文

在任意 JavaScript 函数中,我希望确定上游事件。

事件监听函数没有将事件传递给当前函数。据我所知,window.event 不是 WC3 并且不可用。

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 ) { ... }
}

函数subfunc()如何确定当前事件?

(如果之前问过问题,请道歉 - 似乎一定是这样 - 但无法追踪到它。)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

維他命╮ 2024-12-13 15:33:38

您可以这样做:

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 ) { ... }
}

还有其他方式:

function listener(e) { subfunc.call(e, e.target); }

function subfunc( element ) {
    var event = this; //<- 'this' points to your event object
    switch( event.type ) { ... }
}

You 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 ) { ... }
}
平定天下 2024-12-13 15:33:38

那是不可能的。只有一个属性可以通过另一种可靠的方式访问:event.target = this(前提是从事件侦听器的范围内调用该函数)。

事件对象应该传递给subfunc

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文