jQuery:如何检查鼠标是否在元素上

spb*_*spb 12 jquery

我有一个绑定mouseenter事件的延迟函数:

$(window).load(function(e) {
  var $container = $('.container');
  $container.mouseenter(function(e) {
    //do something
  });
  ...
});
Run Code Online (Sandbox Code Playgroud)

问题是当load事件触发时,鼠标可能已经在元素上,因此在绑定mouseenter事件之前.
我可以通过在页面加载处理程序的末尾放置一些代码来解决这个问题,以便一次性检查鼠标是否在元素内并手动触发mouseenter事件:

// check if the mouse is inside $container
if(mouse is inside $container)
  $container.mouseenter();
Run Code Online (Sandbox Code Playgroud)

如何检查鼠标是否在load事件中的元素上?

我试过$container.is(':hover')但它不起作用.

spb*_*spb 1

我使用jQuery 检测页面加载时的鼠标悬停中的 css 悬停解决方案,因为它不需要 mousemove 事件。