2011-03-19 93 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