事件没有在FireFox中定义,但在Chrome和IE中确定 [英] event is not defined in FireFox, but ok in Chrome and IE

查看:138
本文介绍了事件没有在FireFox中定义,但在Chrome和IE中确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery生成HTML:

I generate HTML using jQuery:

$("<a />")
    .append("demo")
    .click(function () { DemoFunc(event, value.Id) })

这适用于Chrome和IE8,但在FireFox我得到一个错误:事件未定义。
我改变了这样的代码:

This works perfect for Chrome and IE8, but in FireFox I got an error: "event is not defined". I changed the code like this:

.attr("onclick", "DemoFunc(event, " + value.Id + ")")

它适用于Firefox,但不适用于Chrome和IE。 p>

It works for Firefox, but not for Chrome and IE.

DemoFunc = function (e, assocGroupId) {
    var target = (e.target) ? $(e.target) : $(e.srcElement);
    ....
}

为什么!帮帮我!!

推荐答案

在IE和Chrome中, event 正解析为 window.event 。 Firefox没有此属性,而是通过将事件作为参数传递给事件处理函数提供一个事件。 jQuery通过在所有浏览器中提供一个事件对象参数来为你提供这个区别:

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:

$("<a />")
    .append("demo")
    .click(function (evt) { DemoFunc(evt, value.Id) });

这篇关于事件没有在FireFox中定义,但在Chrome和IE中确定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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