jQuery& IE的IE错误超时

时间:2009-11-14 11:18:21

标签: javascript jquery internet-explorer-8 internet-explorer-7 settimeout

在jquery悬停事件中,我使用以下代码下拉菜单:

clearTimeout(hTimeout);
$('#lowermenu').queue('fx', []);
$('#menucenter .current').removeClass('current');
$(this).children('a').addClass('current');        
dTimeout = setTimeout(function($item){slidelower($item)}, 200, $(this)); // This is the bad line

function slidelower($li)
{
    $li.addClass('dropping');
    $lowermenu = $li.children('ul').clone();
    $('#lowermenu:not(:animated)').empty().append($lowermenu).slideDown();
    $('#lowermenu > ul > li:not(:animated)').hover(function()
    {                      
        $(this).children('ul:hidden').css('top', 'auto').slideDown();
    }, function()
    {
        $(this).children('ul:visible').slideUp();
    });
}

我收到以下错误:

网页错误详情

用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 5.1; Trident / 4.0; GTB6.3; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; OfficeLiveConnector.1.3; OfficeLivePatch.0.0) 时间戳:周六,2009年11月14日11:12:46 UTC

消息:'undefined'为null或不是对象

行:81

Char:25

代码:0

URI:[网址在这里]

我怀疑它是由setTimeout引起的 - 我传入第三个参数作为匿名函数的参数。该匿名函数使用闭包调用函数。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

$(this).children('a').addClass('current');   
var that = this;     
dTimeout = setTimeout(function($item){slidelower($item)}, 200, that); // This is the bad line

setTimeout归window个对象所有,因此this引用window。通过使用'that'变量缓存它来保存对外部上下文的引用。

答案 1 :(得分:1)

好的,我发现了问题。 IE中的setTimeout不支持其他参数:

https://developer.mozilla.org/en/window.setTimeout

任务中止。

答案 2 :(得分:1)

以防万一其他人读到这个: 尽管不可能在OP中概述的方法中将参数传递给IE中的setInterval或setTimeout。可以通过使用和匿名函数并传入范围内的参数来实现。

所以OP需要用以下内容替换坏线:

dTimeout = setTimeout(function(){slidelower($item)}, 200);

这是IE中setTimeout的预期语法(2个参数:函数和延迟),但匿名函数会将$ item的值传递给“slidelower”