我的提交按钮有一个小问题。单击它时,我希望它执行不同的操作,但比另一个晚执行两个操作。我一直在尝试使用 Javascript 中的 setTimeout 并将一些我希望它执行的操作放在不同的函数中,但后来我的代码不再工作,因为它不再知道“这”是什么。请问有什么帮助吗?
function test(){
$(this).addClass("voted");
$(this).val('lighted up');
}
$(".stem").click(function() {
$(this).parent().parent().parent().find(".red").addClass("filltherm");
setTimeout(test,1500);
});
最佳答案
将对 this
的引用存储在 _this
中,并将其作为参数传递给 temp
。
function test(el) {
$(el).addClass("voted");
$(el).val('lighted up');
}
$(".stem").click(function() {
var _this = this;
$(this).parent().parent().parent().find(".red").addClass("filltherm");
setTimeout(function () {
test(_this);
}), 1500);
});
关于onClick 中的 Javascript setTimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23242429/