我想使用 jQuery 来确定当前悬停在哪个元素上。它可以是页面上的任何元素,这意味着 mouseover
、mouseout
、mouseenter
和 mouseleave
不适用在这里,因为它们与特定元素相关。
这是一个快速示例:
$(window).bind('mousemove', function() {
if (elementBeingHoveredOver.attr('id') != 'foo') {
// ... (Do some cool stuff here) ...
$(window).unbind('mousemove');
}
});
我知道,我知道,看起来最好将 mouseenter
和 mouseleave
事件处理程序绑定(bind)到 #foo
元素就这样做了,但是鼠标经常移动得太快而无法注册 mouseleave
事件,所以我想尝试这种方式。
关于如何确定 elementBeingHoveredOver
有什么想法吗?
最佳答案
试试这个
$(window).bind('mousemove', function(e) {
if ($(e.target).attr('id') != 'foo') {
// ... (Do some cool stuff here) ...
$(window).unbind('mousemove');
}
});
关于jQuery - 如何检查哪个元素正在悬停(但不是任何特定元素!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480329/