前两天用jQuery写一个页面动态效果,代码如下:
$('.basket').bind('click', event, toggle );
这个代码在chrome和IE中都没有问题,唯独在firefox中显示 Firefox event is undefined
在网上查了查,解释如下(http://stackoverflow.com/a/2974614/2177408):
In IE and Chrome, event
is resolving to window.event
. Firefox doesn't have this property and instead provides an event to an event handler function by passing it as a parameter. jQuery abstracts this difference for you by providing an event object parameter in all browsers:
大意就是这个bind里面的event参数在IE和chrome中是有默认定义的,而在FF中没有,所以解决的方法就是在event之前给他一个定义:
var event = window.event || event;
所以最后的代码就成了:
var event = event || window.event;
$('.basket').bind('click', event, toggle );
顺便把另外一段类似常用的代码也加进来:
var target = event.target || event.srcElement;
http://www.webdeveloper.com/forum/showthread.php?226632-RESOLVED-Firefox-event-is-undefined&p=1075977#post1075977