ReferenceError:事件在Firefox中没有定义错误 [英] ReferenceError: event is not defined error in Firefox

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

问题描述

我为客户做了一个页面,最初我在Chrome中工作,忘记检查它是否在Firefox中工作。现在,我有一个很大的问题,因为整个页面是基于一个脚本,不能在Firefox中工作。



它的基础上的所有链接 code> rel 导致隐藏并显示正确的页面。我不明白为什么这不起作用在Firefox。



例如,页面的ID为 #menuPage #aboutPage 等等。所有的链接都有这样的代码:

 < a class =menuOption ='#homePage'href =#> ; Velkommen< / A> 

在Chrome和Safari中完美运行。

这是代码:

$ $ $ $ $ $ $ $ $ $ $ //主导航

$ b $('。menuOption')。click(function(){

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


});

//隐藏并显示正确的开始菜单
$('。all')。hide();
$('。pizza' ).show();


//在按钮中隐藏和显示使用rel标签
$('。menyCat')。click(function(event){
event.preventDefault();
var categori = $(this).attr('rel');
$('。all')。hide();
$(categori ).fadeIn();
$('html,body')。scrollTo(0,categori);

));


} );


解决方案

您正在声明(某些)事件处理程序不正确:

  $('。menuOption')。click(function(event){//< ---- event参数

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


});

您需要事件作为处理程序的参数。 WebKit遵循IE的事件使用全局符号的旧行为,但Firefox不。当你使用jQuery时,这个库规范化行为,并确保你的事件处理程序传递了事件参数。

澄清:你必须提供一些参数名称;使用事件来清除你的意图,但是你可以把它称为 e 或者 cupcake 或其他任何东西。

还要注意,你可能应该使用从jQuery传入的参数而不是native IE和Safari)是一个(参数)是本地事件对象的jQuery包装。包装是规范化跨浏览器的事件行为。如果你使用全球版本,你不明白。


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.

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.

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

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

It's working perfectly in Chrome and Safari.

Here is the code:

$(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();


});

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.

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.

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天全站免登陆