JQuery why: hover only works once?

I use the following snippet to define in Chrome / Safari and FF if the user is hanging over the anchor.

var isURL = $("a", obj).is(":hover");

I saw different posts about: hover, which is a CSS selector, but what I can’t understand is why the code returns true if obj has 1 link, but throws an unrecognized javascript expression error if there are 2 or more.

Here is the fiddle: hover working: - http://jsfiddle.net/2kyaJ/122/

Same thing but a few elements (doesn't work): - http://jsfiddle.net/2kyaJ/121/

Can someone explain this to me?

By the way, I saw this ... How to check if the mouse is over an element in jQuery?

4 years later, is this even the best and, it would seem, only a way to determine that the user is hanging over an element? If so, can anyone provide an example?

Edit: it was necessary to fish exactly as I needed, but it turned out that it was as simple as working well.

I am currently using it inside a plugin with jQuery 1.9.1, where I start the animation when I hover over the parent element (obj). Hope someone else finds this useful in the future. Use .length instead of .size, since .size is deprecated from version 1.8 onwards.

        function isMouseOver() {
            if ($('a:hover', obj).length != 0) {
                return true;
            } else {
                return false;
            }                           
        }

Using:

var isURL = isMouseOver();
+5
source share
4 answers

:hover http://api.jquery.com/ - - . , Sizzle , , , , .

, , : http://jsfiddle.net/2kyaJ/122/ - jQuery 1.9

, , - , . , . "" mouseover mouseenter. , , CSS :hover.

+5

http://jsfiddle.net/2rU4U/:

setInterval(function(){
    var $sample = $(".sample");

    $sample.each(function(index) {
        if($(this).is(":hover")) {
           $(this).css("background", "yellow");
        }
        else {
           $(this).css("background", "");
        }
    });  
}, 200);

, , , . , ...!

+3

, , , , , . .

, , jQuery 1.7.1, 1.9 2.0.0b1: http://jsfiddle.net/2kyaJ/125/

.is() , , ($(".sample:hover").length, $(".sample").is(":hover")).

, .sample, - , , jsfiddle. , , , - : http://jsfiddle.net/2kyaJ/126/

, - , , 0.2 , .hover(): http://jsfiddle.net/2kyaJ/127/

+1

, - ...

.

$('.sample').hover(function() {
    console.log($this) // $(this) is the currently hovered element
})

JSFiddle: http://jsfiddle.net/jeffshaver/2kyaJ/124/

-2

All Articles