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