2013-01-31 25 views
3

在我的情況下徘徊<div>當這樣前面加上裏面的<div>爲什麼setTimeout()爲0 ms。在我的功能

但在一些DIV我想隱藏懸停的元素之後預謀元素一個元素的影響。

這是我的代碼和它的工作!

HTML:

<div class="one"> 
    <span class="content"></span> 
</div> 


<div class="two"> 
    <span class="content"></span> 
</div> 

的jQuery:

$("div.one > .content").on("hover" , function(){ 
    var this_ = $(this); 
    this_.prev(".top").remove(); 
}); 


$("div.two > .content").on("hover" ,function(){ 
    var this_ = $(this); 
    setTimeout(function(){this_.prev(".top").remove();}) 
}); 

.one.two都是相同的功能,但我想知道爲什麼第二個功能,當我使用setTimeout()和時序影響0毫秒,爲什麼0毫秒影響我的功能?

演示:http://jsfiddle.net/nShCy/

回答

2

這不是0毫秒,這是4 ms delay

答案很簡單:在鼠標懸停後觸發mouseenter,因此您試圖刪除尚不存在的元素。在4ms停頓和懸停回調完成時間不到4 ms的情況下,它按預期工作。

+0

哦!那是 !! – l2aelba

-2
$(function(){ 

      $("div.one > .content").on("hover" , function(){ 
      var this_ = $(this); 
      this_.prev(".top").remove(); 
     }); 


     $("div.two > .content").on("hover" ,function(){ 
      var this_ = $(this); 
      setTimeout(function(){this_.prev(".top").remove();}) 
     }); 

}) 
+2

無可否認,這個問題有點難以理解,但是這根本不能回答。 (請注意,jsfiddle腳本已經在dom準備好了。) – JJJ