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.