2011-08-09 19 views
1

這是我到目前爲止有:有了jQuery,我該如何檢測一個元素是否「鼠標懸停」,以及該元素的任何子元素?

var hoveredElement; //none per default 

;(function($){ 
$.fn.isHovered = function(){ 
    return (hoveredElement.length && $(this)[0] === hoveredElement[0]); 
}; 

})(jQuery); 


$(document).mouseover(function(e){ 
    hoveredElement = $(e.target); 
}); 


$(document).mouseover(function(e){ 
    console.log($(this).isHovered()); 
}); 

基本上我有以下結構:

<div id='one'> 
    <div id="two"> 
     <div id="three"> 
      three 
     </div> 
    </div> 
</div> 

當我鼠標了兩個,我想返回true無論是#two或#三,我在徘徊。

我該如何做到這一點?

+1

任何理由你爲什麼不使用['hover()'](http://api.jquery.com/hover/)? – Shef

回答

1

使用jQuery .hover() API:http://api.jquery.com/hover/並且您應該能夠通過$(this)查看當前對象。

喜歡的東西:

$('div').hover( 
function() { console.log('hovering over %o', $(this) }, 
function() { console.log('leaving') } 
); 
+1

那真的不是我想要的。我需要一個返回bool元素或其子元素是否被徘徊的函數。 –

+0

就像我說過的,我不是在執行懸停操作,而是根據是否懸停該項目來更改另一個腳本的流動。 –

+0

我設法創建了一個使用這個設置變量的函數。謝謝 –

相關問題