jQuery 使用 .each 匹配元素并使用 setTimeout 延迟其处理?
尝试匹配元素并使用 .each() 按设定的时间间隔触发它们,但无法正确获取对匹配元素的引用。
这匹配正确,但它们同时被触发:
$("div[id^='data_field'] input:submit").each(function(index) { setTimeout(console.log($(this)),1000*index ); });
这正确地触发它们,但元素匹配出错:
$("div[id^='data_field'] input:submit").each(function(index) { setTimeout(function(){ console.log($(this)); },1000*index ); });
如何正确?是的,它需要与 .each...
Trying to match elements and fire them off at set intervals with .each(), but can't get the reference to the matched element right.
This matches correctly, but they're all fired at the same time:
$("div[id^='data_field'] input:submit").each(function(index) { setTimeout(console.log($(this)),1000*index ); });
This fires them correctly, but element matching goes wrong:
$("div[id^='data_field'] input:submit").each(function(index) { setTimeout(function(){ console.log($(this)); },1000*index ); });
How to get it right? And yes it needs to be with .each...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
满身野味2024-10-27 07:30:53
下面是使用 jQuery 的 .delay()< 进行延迟的
.each
示例/code>功能:
$('#show-articles').click(function(event) {
var delayInterval = 1;
$('.articles').each(function(){
$(this).delay(delayInterval * 50).fadeIn()
delayInterval++;
})
event.preventDefault();
})
……当然,您需要适应您的确切需求。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当作用域为 setTimeout 内的函数时,“this”会变成其他东西。如果您将信息存储在 setTimeout 之外的
$(this)
中,那么它就可以工作。看看这个小提琴 http://jsfiddle.net/VZwFB/1/"this" becomes something else when scoped to the function inside the setTimeout. If you store the information in
$(this)
outside of the setTimeout then it works. Take a look at this fiddle http://jsfiddle.net/VZwFB/1/