How do I check if the mouse is over an element in jQuery?

前端 未结 24 2117
不思量自难忘°
不思量自难忘° 2020-11-22 08:10

Is there a quick & easy way to do this in jQuery that I\'m missing?

I don\'t want to use the mouseover event because I\'m already using it for something else. I

24条回答
  •  孤街浪徒
    2020-11-22 08:58

    In jQuery you can use .is(':hover'), so

    function IsMouseOver(oi)
    {
       return $(oi).is(':hover');
    }
    

    would now be the most concise way to provide the function requested in the OP.

    Note: The above does not work in IE8 or lower

    As less succinct alternative that does work in IE8 (if I can trust IE9's IE8 modus), and does so without triggering $(...).hover(...) all over the place, nor requires knowing a selector for the element (in which case Ivo's answer is easier):

    function IsMouseOver(oi)
    {
        return oi.length && 
               oi.parent()
                 .find(':hover')
                 .filter(function(s){return oi[0]==this})
                 .length > 0;
    }
    

提交回复
热议问题