如何检查鼠标是否在 jQuery 中的元素上? [英] How do I check if the mouse is over an element in jQuery?

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

问题描述

有没有快速&在我缺少的 jQuery 中执行此操作的简单方法是什么?

Is there a quick & easy way to do this in jQuery that I'm missing?

我不想使用 mouseover 事件,因为我已经将它用于其他用途.我只需要知道鼠标在给定时刻是否在一个元素上.

I don't want to use the mouseover event because I'm already using it for something else. I just need to know if the mouse is over an element at a given moment.

我想做这样的事情,如果只有IsMouseOver"功能:

I'd like to do something like this, if only there was an "IsMouseOver" function:

function hideTip(oi) {
    setTimeout(function() { if (!IsMouseOver(oi)) $(oi).fadeOut(); }, 100);
}

推荐答案

设置鼠标退出到淡出的超时时间,并将返回值存储到对象中的数据中.然后onmouseover,如果数据中有值就取消超时.

Set a timeout on the mouseout to fadeout and store the return value to data in the object. Then onmouseover, cancel the timeout if there is a value in the data.

移除淡出回调的​​数据.

Remove the data on callback of the fadeout.

使用 mouseenter/mouseleave 实际上更便宜,因为当儿童鼠标悬停/鼠标移出触发时,它们不会为菜单触发.

It is actually less expensive to use mouseenter/mouseleave because they do not fire for the menu when children mouseover/mouseout fire.

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

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