为什么Firefox会说window.event是未定义的? (带有添加的事件侦听器的调用函数) [英] Why Firefox says that window.event is undefined? (call function with added event listener)

查看:69
本文介绍了为什么Firefox会说window.event是未定义的? (带有添加的事件侦听器的调用函数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这部分我很麻烦:

var ex = {
  exampl: function(){
    var ref=window.event.target||window.event.srcElement; // here
    alert(ref.innerHTML); // (example)
  }
}

此函数的调用方式为:

document.body.childNodes[0].addEventListener('mouseover',ex.exampl,true);

只有Firefox表示未定义window.event ...

Only Firefox says that window.event isn't defined...

我不知道该怎么做才能使其正常工作.它在webkit浏览器和Opera中非常有效(我无法在MSIE中对其进行检查,而且我也不在乎).

I don't know what to do, to make it work. It works very well in webkit browsers and opera (I can't check it in MSIE and I don't care about it).

为什么会发生?

推荐答案

尝试使用传递的参数(在本例中为e)获取事件.我对此进行了测试,并且chrome支持window.evente.

try getting the event using the parameter passed (named e in this case). i tested this and both window.event and the e is supported in chrome.

尝试检查两者(以存在者为准)

var ex = {
  exampl: function(e){

    console.log(window.event);
    console.log(e);  

    //check if we have "e" or "window.event" and use them as "evt"
    var evt = e || window.event    

  }
}

这篇关于为什么Firefox会说window.event是未定义的? (带有添加的事件侦听器的调用函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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