使用JQuery将鼠标悬停在元素上2秒钟时如何触发点击? [英] How do I trigger a click when hovering over a element for 2 seconds, using JQuery?
问题描述
如果将鼠标悬停在a.menu-arrow
上,如何使用JQuery触发此内联函数
onClick="showMenu('mnu_searches', event, 0, this)
?用户将鼠标悬停在元素上2秒钟后,我需要触发一次点击吗?
How do I trigger this inline functiononClick="showMenu('mnu_searches', event, 0, this)
using JQuery...if I hover over a.menu-arrow
? I need to trigger a click after the user has been hovering over the element for 2 seconds?
任何帮助将不胜感激,谢谢
Any help would be greatly appreciated, Thanks
推荐答案
您可以创建并清除2秒计时器,如下所示:
You can create and clear a 2 second timer, like this:
$("a.menu-arrow").hover(function() {
$.data(this, "timer", setTimeout($.proxy(function() {
$(this).click();
}, this), 2000));
}, function() {
clearTimeout($.data(this, "timer"));
});
您可以在这里尝试.通过使用 $.data()
,我们可以存储每个元素的超时 为避免出现任何问题并清除正确的计时器.其余的只是在进入元素时设置2秒计时器,并在离开元素时将其清除.因此,如果您停留2000毫秒,则会触发 .click()
,如果您停止计时,请清除计时器
You can give it a try here. By using $.data()
we're storing a timeout per element to avoid any issues and clear the correct timer. The rest is just setting a 2 second timer when entering the element, and clearing it when leaving. So if you stay for 2000ms, it fires a .click()
, if you leave it stops clear the timer.
这篇关于使用JQuery将鼠标悬停在元素上2秒钟时如何触发点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!