我想在以下代码中添加一个 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 的初学者。
最佳答案
this
的含义在 setTimeout()
中是不同的。您需要在变量中引用所需的 this
或首先获取 ul
并引用它。
var th = this;
setTimeout(function() {
$('ul', th).fadeOut(100);
});
或
var $ul = $('ul',this);
setTimeout(function() {
$ul.fadeOut(100);
});
关于jquery - 将 setTimeout 添加到现有 jQuery 脚本的语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3497586/