检查元素是否悬停在上面

check if an element is being hovered over

本文关键字:在上面 悬停 是否 元素 检查      更新时间:2023-09-26

我想检查元素是否悬停在上面。我收到此错误:

Syntax error, unrecognized expression: unsupported pseudo: hover

当我使用此代码时:

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

我也试过这个:

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

这也行不通。有没有其他方法可以做到这一点。谢谢。

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

jQuery内置了此功能。请参阅 .hover() 上的文档

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