【问题标题】:jQuery undefined variable in IE8IE8中的jQuery未定义变量
【发布时间】:2012-07-04 19:55:43
【问题描述】:

我为滑块创建了一个 jQuery 迷你脚本。它在 Firefox、Chrome、IE9 中完美运行,但当我将鼠标悬停在 IE7、IE8 中时。它显示一个警报错误(未定义的 current_hover_item)。

$('.nav-item').hover(function(){
        var current_hover_item = $(this).children().attr('rel');
        $('.show-item').hide();
        $(current_hover_item).fadeIn(1000);
        $(this).animate({"width": "+=10px", opacity: 0.6}, 500, function(){
             $(this).animate({"width": "-=10px", opacity: 1}, 500);
        });
}, function(){
        $(current_hover_item).fadeOut(1000);
        return false;
});

我尝试删除 (var) 关键字,但是当我悬停时,它不会显示所有内容。 请帮我。谢谢。

【问题讨论】:

    标签: javascript jquery undefined


    【解决方案1】:

    current_hover_item 在第二个函数中未定义,因为第一个函数中的定义在 closure 中。

    您需要在两个函数中声明current_hover_item。或者,由于您只在每个函数中使用该变量一次,因此将其完全删除,如下所示:

    $('.nav-item').hover(function(){
            $('.show-item').hide();
            $($(this).children().attr('rel')).fadeIn(1000);
            $(this).animate({"width": "+=10px", opacity: 0.6}, 500, function(){
                 $(this).animate({"width": "-=10px", opacity: 1}, 500);
            });
    }, function(){
            $($(this).children().attr('rel')).fadeOut(1000);
            return false;
    });
    

    或者最好重构你的代码。

    【讨论】:

    • 谢谢。我已经尝试过您的代码,但在我的上下文中,它不起作用。
    【解决方案2】:

    我今天早些时候遇到了这个问题。我通过在任何 jquery 函数(包括 .ready())之外声明变量来修复它,然后将它设置在你需要它的地方(你当前声明它的地方)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多