2013-02-08 20 views
0

我基本上是試圖在jQuery中應用下滑動導航。 我用這個代碼:

<script> 
    $(document).ready(function() { 
     $(".menu").hover(function(){ 
      $(".submenu").animate({ height: 'show', opacity: 'show' }, 'slow'); 
     }, function(){ 
      $(".submenu").animate({ height: 'hide', opacity: 'hide' }, 'slow'); 
     }); 
    }); 
</script> 

但是,當我將鼠標懸停在一個.menu格,那麼所有的.submenu DIV得到滑下。所以我試着用$(this)來完成它。但我不知道該怎麼做。

+0

請張貼的HTML這適用於。 – j08691 2013-02-08 17:00:13

回答

0

如今,要綁定的事件是這樣的:

$(document.body).on({ 
     mouseover : function(e) { 
      $(this).find(".submenu")... 
     }, 
     mouseout : function (e) { 
      //... 
      } 
     //,... 
    },".menu"); 
+0

雖然在添加完全不同的事件時情況屬實,但「hover」會爲您做到這一點。 – 2013-02-08 17:06:40

+0

但它確實佔用了比委託+內容更多的內存不能動態追加 – mikakun 2013-02-08 17:15:55

2

您需要使用this的上下文中搜索.submenu元素,像這樣:

$(document).ready(function() { 
    $(".menu").hover(function(){ 
     $(".submenu", this).animate({ height: 'show', opacity: 'show' }, 'slow'); 
    }, function(){ 
     $(".submenu", this).animate({ height: 'hide', opacity: 'hide' }, 'slow'); 
    }); 
}); 
+0

這裏是鏈接到文檔:http://api.jquery.com/jQuery/#jQuery1 – Styxxy 2013-02-08 17:02:43

相關問題