以下代码由于某种原因无法工作,它不喜欢 setTimeout 部分,相反,我能做的唯一工作版本是没有延迟...
jQuery(".divhere").hide();
if(jQuery('.divhere').length >= 1){
jQuery(".divhere").each(function() {
setTimeout(function(el) {
jQuery(this).slideDown("slow");
jQuery(this).show();
}, 1000);
});
}
唯一有效的是:
jQuery(".divhere").hide();
if(jQuery('.divhere').length >= 1){
jQuery(".divhere").each(function() {
jQuery(this).slideDown("slow");
jQuery(this).show();
});
}
最佳答案
this
上下文在 setTimeout
场景中丢失。如果您尝试在 setTimeout
中使用 console.log(this)
,您将找到 window
对象。
Use
.bind
: JavaScript’s Bind Allows Us to Set the this Value on Methods
试试这个:
jQuery(".divhere").hide();
if(jQuery('.divhere').length >= 1){
jQuery(".divhere").each(function() {
setTimeout(function(el) {
jQuery(this).slideDown("slow");
jQuery(this).show();
}.bind(this), 1000);
});
}
关于javascript - 为什么我的每个类使用的 jQuery 片段不支持 setTimeout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34312268/