2011-03-19 98 views
-1

我有以下的jQuery代码 -Jquery的的setTimeout不工作

$('div#mid_number_of_mail').mouseover(function() { 

    setTimeout(function() { 
     $('div.element_popup' ,this).stop().animate({ 
      opacity : '1' 
     }, 250, 'linear', function() { }); 
    }, 5000); 

}); 

但我不知道为什么它不能正常工作。有人可以帮我解决这个问题吗?

在此先感谢!

+2

它以什么方式不起作用?萤火虫说什么? – 2011-03-19 08:29:03

+0

我不知道,因为我是jQuery的新手。我只是没有看到元素消失和淡出。 – 2011-03-19 08:46:13

回答

5

因为thiswindow不是具有给定ID的div。

this基于的上下文相关性如何调用函数

由于函数是通过setTimout调用的,所以它没有对象上下文,所以使用默认对象:window

您希望this与鼠标悬停功能相同,因此您需要将其副本保存在其他变量中。

$('div#mid_number_of_mail').mouseover(function() { 
    var that = this; // Take the this from this context and keep it for other functions 
    setTimeout(function() { 
        $('div.element_popup', that).stop().animate({ 
+0

谢谢大卫它的工作!非常感谢您的帮助! – 2011-03-19 08:32:13

+0

David .........? – 2014-12-12 17:18:37

2

尝试使用:

setTimeout(function(ele) {}, 2000, $(this)); 

在你的 “俄勒”,你就会有一个参考,以 “$(本)”。

+0

谢谢你的工作。非常感谢您的帮助Akarun! – 2011-03-19 08:32:38

+0

不客气。 – Akarun 2012-03-21 17:43:46