如何检查鼠标是否在 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.