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