javascript事件处理程序中的“事件"参数来自何处? [英] Where does the 'event' param come from in javascript event handlers?

查看:271
本文介绍了javascript事件处理程序中的“事件"参数来自何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到javascript代码,其中动态分配了事件处理程序(例如onmousemove). 示例:

Often I see javascript code where event handlers (like onmousemove) are assigned dynamically. Example:

document.getElementById('foo').onmousemove = function(e)
{ /* do some stuff with event e */ }

显然,此"e"参数是某种事件对象.它是从哪里来的,例如:调用函数时,谁或什么定义了这个'e'参数是什么,我也可以在静态html中执行此操作吗? 我的意思是这样:

Apparently this 'e' parameter is some kind of event object. Where does that come from, as in: who or what defines what this 'e' parameter is when the function is called, and can I also do this in static html? I mean like this:

<div id='foo' onmousemove='Bla(e)'> ... </div>

我应该为'e'填写什么以得到相同的事件?而且我还可以将其与更多参数结合起来,例如

What should I fill in for 'e' to get that same event thing? And can I also combine that with more parameters, like

<div id='foo' onmousemove='Bla(this,e,4)'> ... </div>

再次将e假定为事件对象吗?

where e is, again, supposed to be the event object?

推荐答案

事件对象存储在任何事件处理程序内的window.event中,因此您不必担心处理程序是否将其接受为参数.

The event object is stored in window.event inside of any event handler, so you do not need to worry about your handler conforming to accepting it as a parameter.

在第二个和第三个示例中,e参数将作为未定义参数传递,因为该范围内不存在变量e(除非您具有全局e).

In your second and third examples, the e parameter will be passed as undefined because no variable e exists in that scope (unless you have a global e).

这篇关于javascript事件处理程序中的“事件"参数来自何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆