将 setTimeout 添加到现有 jQuery 脚本的语法问题
我想在以下代码中添加 setTimeout ,以便在执行 fadeOut 效果之前有一个短暂的暂停。
$(document).ready(function() {
$('#menu li').hover(
function() {
$('ul', this).slideDown(50);
},
function() {
$('ul', this).fadeOut(100);
}
);
});
这就是我正在尝试的,但我猜语法一定是错误的:
$(document).ready(function() {
$('#menu li').hover(
function() {
$('ul', this).slideDown(50);
},
function() {
setTimeout(function() {
$('ul,' this).fadeOut(100);
});
}
);
});
抱歉,如果这是一个愚蠢的问题。我是 jQuery 的初学者。
I want to add a setTimeout to the following code so that there's a short pause before the fadeOut effect executes.
$(document).ready(function() {
$('#menu li').hover(
function() {
$('ul', this).slideDown(50);
},
function() {
$('ul', this).fadeOut(100);
}
);
});
This is what I'm trying, but I'm guessing the syntax must be wrong:
$(document).ready(function() {
$('#menu li').hover(
function() {
$('ul', this).slideDown(50);
},
function() {
setTimeout(function() {
$('ul,' this).fadeOut(100);
});
}
);
});
Sorry if this is a dumb question. I'm a beginner with jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
this
的含义在setTimeout()
中是不同的。您需要在变量中引用所需的this
或首先获取ul
并引用它。或者
The meaning of
this
is different within asetTimeout()
. You need to reference the desiredthis
in a variable or get theul
first and reference that.or
您可能还需要使用
clearTimeout()
(如果您快速将鼠标悬停在/退出),类似这样的操作会起作用:这使用
$.data()
,目前有400ms的延迟,只需相应调整即可。You could also need to clear the timeout when going over it using
clearTimeout()
(in case you hover in/out fast), something like this would work:This stores/retrieves the timer ID using
$.data()
, and currently has a 400ms delay, just adjust accordingly.