jQuery:如何检查鼠标是否在元素上 [英] jQuery: How to check if the mouse is over an element

查看:404
本文介绍了jQuery:如何检查鼠标是否在元素上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a deferred function that binds a mouseenter event:

$(window).load(function(e) {
  var $container = $('.container');
  $container.mouseenter(function(e) {
    //do something
  });
  ...
});

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

The problem is the mouse might already be over the element when the load event fires, hence before the mouseenter event was bound.
I can get around this by putting some code at the end of the page load handler to do a one-time check if the mouse is inside the element and trigger the mouseenter event manually:

// check if the mouse is inside $container
if(mouse is inside $container)
  $container.mouseenter();

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

How can I check if the mouse is over the element in the load event?

我尝试了$container.is(':hover'),但是它不起作用.

I tried $container.is(':hover') but it doesn't work.

推荐答案

我在

I went with the css hover solution in Detect mouse hover on page load with jQuery because it doesn't require the mousemove event.

这篇关于jQuery:如何检查鼠标是否在元素上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆