ReferenceError:Firefox 中的事件未定义错误 [英] ReferenceError: event is not defined error in Firefox

查看:24
本文介绍了ReferenceError:Firefox 中的事件未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为客户制作了一个页面,最初我在 Chrome 中工作,但忘记检查它是否在 Firefox 中工作.现在,我遇到了一个大问题,因为整个页面都基于一个在 Firefox 中不起作用的脚本.

I've made a page for a client and I initially was working in Chrome and forgot to check if it was working in Firefox. Now, I have a big problem because the whole page is based upon a script that doesn't work in Firefox.

它基于所有具有 rel 的链接",导致隐藏和显示正确的页面.我不明白为什么这在 Firefox 中不起作用.

It's based on all "links" that have a rel that leads to hiding and showing the right page. I don't understand why this isn't working in Firefox.

例如页面具有 id #menuPage#aboutPage 等等.所有链接都有这个代码:

For instance pages have the id #menuPage, #aboutPage and so on. All links have this code:

<a class="menuOption" rel='#homePage' href="#">Velkommen</a> 

它在 Chrome 和 Safari 中完美运行.

It's working perfectly in Chrome and Safari.

代码如下:

$(document).ready(function(){

//Main Navigation


$('.menuOption').click(function(){

  event.preventDefault();
  var categories = $(this).attr('rel');
  $('.pages').hide();
  $(categories).fadeIn();


});

// HIDES and showes the right starting menu
    $('.all').hide();
    $('.pizza').show();


// Hides and shows using rel tags in the buttons    
    $('.menyCat').click(function(event){
        event.preventDefault();
        var categori = $(this).attr('rel');
        $('.all').hide();
        $(categori).fadeIn();
        $('html,body').scrollTo(0, categori);

    });


}); 

推荐答案

您错误地声明了(部分)事件处理程序:

You're declaring (some of) your event handlers incorrectly:

$('.menuOption').click(function( event ){ // <---- "event" parameter here

    event.preventDefault();
    var categories = $(this).attr('rel');
    $('.pages').hide();
    $(categories).fadeIn();


});

您需要事件"作为处理程序的参数.WebKit 遵循 IE 对事件"使用全局符号的旧行为,但 Firefox 没有.当您使用 jQuery 时,该库会规范行为并确保您的事件处理程序传递事件参数.

You need "event" to be a parameter to the handlers. WebKit follows IE's old behavior of using a global symbol for "event", but Firefox doesn't. When you're using jQuery, that library normalizes the behavior and ensures that your event handlers are passed the event parameter.

编辑 —澄清:您必须提供一些参数名称;使用 event 可以明确您的意图,但您可以将其称为 ecupcake 或其他任何名称.

edit — to clarify: you have to provide some parameter name; using event makes it clear what you intend, but you can call it e or cupcake or anything else.

还要注意,您可能应该使用从 jQuery 传入的参数而不是本机"参数(在 Chrome、IE 和 Safari 中)的原因是该参数(参数)是围绕本机事件对象的 jQuery 包装器.包装器用于规范跨浏览器的事件行为.如果您使用全球版本,则不会.

Note also that the reason you probably should use the parameter passed in from jQuery instead of the "native" one (in Chrome and IE and Safari) is that that one (the parameter) is a jQuery wrapper around the native event object. The wrapper is what normalizes the event behavior across browsers. If you use the global version, you don't get that.

这篇关于ReferenceError:Firefox 中的事件未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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