开发者

With jQuery, how do i detect if an element is 'mouseovered' and any child elements of that element?

开发者 https://www.devze.com 2023-03-26 20:51 出处:网络
Here is what I have so far: var hoveredElement; //none per default ;(function($){ $.fn.isHovered = function(){

Here is what I have so far:

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() );
});

Basically I have the following structure:

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

When I mouse over two, i'd like to return true whether it is #two or #three that I am mousing over.

How do i accomplish this?


Use the jQuery .hover() API: http://api.jquery.com/hover/ and you should be able to view the current object via $(this).

Something like:

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


Try to look into mouseenter, and mouseleave it should give you what you are looking for


Your code works here: http://jsfiddle.net/maniator/LjUu7/2/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号