简体   繁体   中英

check if an element is being hovered over

I want to check if a element is being hovered over. I get this error:

Syntax error, unrecognized expression: unsupported pseudo: hover

when I use this code:

 $('.class').blur(function(){
    if(!$(this).is(':hover')){
       //element not being hovered over
    }
 });

i also tried this:

 $('.class').blur(function(){
    if($(this+":hover").length === 0){ 
       //element not being hovered over
    }
 });

this also does not work. Is there any other way of doing this. Thanks.

$(".class").mouseover(function(){   
    $(this).attr('checked',true);   
    });  
});

jQuery has this functionality built-in. See documentation on .hover()

$(".class").hover(
  function() {
    // item is being hovered over
    $(this).addClass('hover');
  }, function() {
    // item is no longer being hovered over
    $(this).removeClass('hover');
  }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM