2013-05-09 125 views
1

我有一個單擊按鈕後會出現的元素。我現在想要的是當我的鼠標指針離開元素時隱藏元素。這怎麼可能?由於JS當鼠標離開內容區域時隱藏html內容

JS腳本:

  $(document).ready(function() { 

      $('.hide').hide(); 



      $('.button').click(function() { 
      $(this).closest('div').find('.hide').toggle(400); 
      return false;   
      }); 

HTML代碼:

<a class="button" >More</a> 

<div class="hide" ><img src="images/icon.png" /></div> 
//I want this div hide element to hide after my mouse pointer leave this div area 

非常感謝你:d

+0

檢查更新的答案。 – 2013-05-09 08:16:25

回答

7
$("div.hide").mouseleave(function(){ 
    $(".hide").hide(); 
}); 

對於再次展示了你可以使用的mouseenter()之類的DIV內容:

$("div.hide").mouseenter(function(){ 
    $(".hide").show(); 
}); 

,或者您也可以這樣做:(對於切換使用)

$("div.hide").mouseleave(function(){ 
    $(".hide").hide(); 
}).mouseenter(function(){ 
    $(".hide").show(); 
}); 
0

你嘗試使用mouseout事件?

http://api.jquery.com/mouseout/

+3

儘管可以使用'mouseout',但由於事件冒泡,此事件類型可能會導致許多頭痛。查看['.mouseleave()'](http://api.jquery.com/mouseleave/)的討論以獲得有用的選擇。 – SoonDead 2013-05-09 08:30:04

+0

我同意。感謝信息..;) – 2013-05-09 08:41:20