在鼠标悬停时向jquery事件添加延迟
我正在尝试为孩子的鼠标悬停事件添加一个简单的延迟,并遇到一些困难. (仍在学习!)
I am trying to add a simple delay to a mouseover event of a child and having difficulties. (Still learning!)
这使我能够在延迟后显示弹出窗口,但同时显示所有弹出窗口:
This enables me to show the popup after a delay, but shows all of them simultaneously:
onmouseover='setTimeout(function() { $(\".skinnyPopup\").show(); }, 600)'
,这可以立即显示我想要的弹出窗口:
and this works to show only the popup I want with no delay:
onmouseover='$(this).children(\".skinnyPopup\").show()'
但组合不能:
onmouseover='setTimeout(function() { $(this).children(\".skinnyPopup\").show(); }, 600)'
任何帮助将不胜感激.谢谢!
Any help would be appreciated. Thanks!
您需要定义this
在执行时是什么,这样可以正常工作:
You need to define what this
is when it executes, something like this would work:
setTimeout($.proxy(function() { $(this).children(".skinnyPopup").show(); }, this), 600)
或者只需使用 .delay()
,如下所示:
Or just use .delay()
, like this:
$(this).children(".skinnyPopup").delay(600).show(0);
以上两种方法都是快速修复,建议您不要使用内联处理程序,并查看不引人注目方法(请参见此 Russ Cam 回答(出于某些重要原因),例如:
Both of the above are quick fixes, I suggest you move away from inline handlers and check out an unobtrusive method (see this answer by Russ Cam for some great reasons), for example:
$(function() {
$('selector').mouseover(function() {
$(this).children(".skinnyPopup").delay(600).show(0);
});
});