javascript - IE 错误与 jQuery 和超时

标签 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

消息:“未定义”为 null 或不是对象

行:81

字符:25

代码:0

URI:[网址位于此处]

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

有人可以帮忙吗?

最佳答案

$(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”变量缓存来保存对外部上下文的引用。

关于javascript - IE 错误与 jQuery 和超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1733974/

相关文章:

css - IE8 :focus causes issue - need to double click

javascript - 在 GruntJS 任务中使用 ‘this.files’ 时出现问题

javascript - DHTML 弹出窗口

单击时 Jquery Nav 消失

javascript - 为多个动态生成的选择提交时的 Jquery 选择值

javascript - 多个 CSS pie + overlay 的问题

javascript - 更改其他选择时更改选择选项

javascript - 将 <BR> 标签转换为 Char(10)

Javascript..可能存在引用值问题

html - 内联图像支持的差异?