简体   繁体   English

在Firefox中使用javascript / jQuery事件处理程序

[英]Using javascript / jQuery event handler in Firefox

I have a jQuery function that submits a form via menu navigation functions: 我有一个通过菜单导航功能提交表单的jQuery函数:

$(function () {
    $('#sidebarmenu1 a').on('click', function () {
        var url = $(this).attr('href');
        $('#myform').attr('action', url);
        $("#myform").submit();
        if (event.preventDefault) { event.preventDefault(); } else { event.returnValue = false; }
    })
});

This section: 这个部分:

if (event.preventDefault) 
{ event.preventDefault(); } 
else 
{ event.returnValue = false; }

Prevents the default action of the sidebar button (I think - still new to this) ie to simply navigate to a page. 防止侧边栏按钮的默认操作(我认为-对此仍然很新),即仅导航到页面。

It is written in this way to keey IE happy, because preventDefault isn't defined for IE (might be using incorrect terminology there, but IE doesn't like preventDefault.) 这样做是为了让IE保持快乐,因为未为IE定义preventDefault(可能在那里使用了不正确的术语,但IE不喜欢preventDefault。)

However now this throws up an error in firefox, because (as I read on other stack questions) event is not globally defined for FF! 但是现在这在firefox中引发了一个错误,因为(正如我在其他堆栈问题上所读到的)事件未为FF全局定义! I get the following error: 我收到以下错误:

ReferenceError: event is not defined ReferenceError:事件未定义

Now acorrding to this stack question: event is not defined in FireFox, but ok in Chrome and IE 现在根据这个堆栈问题: 在FireFox中未定义事件,但在Chrome和IE中确定

In IE and Chrome, event is resolving to window.event. 在IE和Chrome中,事件解析为window.event。 Firefox doesn't have this property and instead provides an event to an event handler function by passing it as a parameter. Firefox没有此属性,而是通过将其作为参数传递给事件处理程序函数。 jQuery abstracts this difference for you by providing an event object parameter in all browsers. jQuery通过在所有浏览器中提供事件对象参数来为您抽象化这种差异。

But I thought I was using jQuery here and am still getting the issue. 但是我以为我在这里使用jQuery,但仍然遇到问题。

Sorry if I'm making basic mistakes, self teaching myself js and jQ. 抱歉,如果我犯了基本错误,请自学js和jQ。 Any help much appreciated, thanks! 任何帮助,不胜感激,谢谢!

This should do.. 这应该做..

$(function() {
    $('#sidebarmenu1 a').on('click', function(e) {
        var evt = e || window.event;
        var url = $(this).attr('href');
        $('#myform').attr('action', url);
        $("#myform").submit();
        evt.preventDefault();
    })
});​

If you use the event object jQuery passes to the event handler you wont have problems 如果您使用事件对象jQuery传递给事件处理程序,则不会有问题

$('#sidebarmenu1 a').on('click', function (event) {
    var url = $(this).attr('href');
    $('#myform').attr('action', url);
    $("#myform").submit();
    event.preventDefault();
})

If I had a couple of more points I would up-vote the second answer (passing in the jQuery 'event' argument). 如果我还有其他几点,我将投票赞成第二个答案(传递jQuery'event'参数)。

I had unknowingly relied on the global event defined by browsers other than Firefox. 我在不知不觉中依赖于除Firefox之外的浏览器定义的全局事件。 I went back through my code and ensured I was always specifying the event parameter on all click events. 我回顾了代码,并确保始终在所有click事件上指定event参数。

Eg 例如

            $('#situationTextInput')
                .val('')
                .fadeTo(1000, 1.0)
                .focus()
                .off('keydown')
                .on('keydown', function (event) {
                    VsUtils.handleSpanishTextKeydown(event);
                });

I've also gotten into the habit of calling .off prior to .on as most of my code is using single pages, reusing content. 我还养成了在.on之前调用.off的习惯,​​因为我的大多数代码都使用单个页面,重复使用内容。 Without the .off I was inadvertently stacking up events (even if they were the same callback) in subsequent steps of the dialog. 没有.off,我在对话框的后续步骤中会无意间堆积事件(即使它们是相同的回调)。

A side effect of changing my code to pass on 'event' directly to the handler was the binding to 'this.' 更改代码以将“事件”直接传递到处理程序的副作用是绑定到“ this”。 changed. 改变了。 I chose to refactor the code to change 'this.' 我选择重构代码以更改“ this”。 references to 'event.currentTarget.' 引用“ event.currentTarget”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM