我为 slider 创建了一个 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) 关键字,但当我将鼠标悬停时,它不会显示所有内容。 请帮我。谢谢。
最佳答案
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;
});
或者最好重构您的代码。
关于javascript - IE8 中 jQuery undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11334793/