This question already has an answer here:
这个问题在这里已有答案:
- Why is 'event' variable available even when not passed as a parameter? 1 answer
为什么即使未作为参数传递,'event'变量也可用? 1个答案
on pressing a link, I am calling a javascript function
按下一个链接,我正在调用一个javascript函数
<a id="eventFiringLink" href="javascript:functionName()">
and in another place of the code for a button I am binding the functionName()
function as a listener for a click event.
在按钮代码的另一个地方,我将functionName()函数绑定为click事件的侦听器。
<button id="someButton">
binding click event by jquery
通过jquery绑定click事件
$("#someButton").bind('click',functionName);
and the function description is exactly like below.,
功能描述如下所示。,
functionName(e)
{
if(event.target.id== "eventFiringLink")
{
console.log("do this");
}
else
{
console.log("do that");
}
}
when I clicked the link, I thought that I would get
当我点击链接时,我以为我会得到
"TypeError: Cannot read property 'target' of undefined",
“TypeError:无法读取未定义的属性'target'”,
but to my surprise the code got executed and printed
但令我惊讶的是,代码被执行并打印出来
"do this"
in the console.
在控制台中。
How is that possible.?
怎么可能。?
2 个解决方案
#1
You're accessing the global window.event
. Support for this object varies from browser to browser.
您正在访问全局window.event。对于此对象的支持因浏览器而异。
See http://www.quirksmode.org/js/events_access.html
The parameter passed by jQuery is normalized to avoid these browser inconsistencies, but as you realized, you weren't accessing that parameter, so you were getting the raw global event object instead.
jQuery传递的参数被规范化以避免这些浏览器的不一致,但正如您所意识到的那样,您没有访问该参数,因此您获得了原始的全局事件对象。
#2
You are actually accessing the global object 's property here which is window. so when u access event it is actually window.event. But beware, modern browsers may not all support this as this is internal to browser's event handling mechanism and it's advisable to take the event as an argument. In fact, in most cross browser libraries the common method of accessing the event object is as below:
您实际上是在这里访问全局对象的属性窗口。所以当你访问事件时,它实际上是window.event。但请注意,现代浏览器可能并非都支持这一点,因为这是浏览器事件处理机制的内部,建议将事件作为参数。实际上,在大多数跨浏览器库中,访问事件对象的常用方法如下:
if(!e){ e = window.event; }
#1
You're accessing the global window.event
. Support for this object varies from browser to browser.
您正在访问全局window.event。对于此对象的支持因浏览器而异。
See http://www.quirksmode.org/js/events_access.html
The parameter passed by jQuery is normalized to avoid these browser inconsistencies, but as you realized, you weren't accessing that parameter, so you were getting the raw global event object instead.
jQuery传递的参数被规范化以避免这些浏览器的不一致,但正如您所意识到的那样,您没有访问该参数,因此您获得了原始的全局事件对象。
#2
You are actually accessing the global object 's property here which is window. so when u access event it is actually window.event. But beware, modern browsers may not all support this as this is internal to browser's event handling mechanism and it's advisable to take the event as an argument. In fact, in most cross browser libraries the common method of accessing the event object is as below:
您实际上是在这里访问全局对象的属性窗口。所以当你访问事件时,它实际上是window.event。但请注意,现代浏览器可能并非都支持这一点,因为这是浏览器事件处理机制的内部,建议将事件作为参数。实际上,在大多数跨浏览器库中,访问事件对象的常用方法如下:
if(!e){ e = window.event; }