jQuery 使用 .each 匹配元素并使用 setTimeout 延迟其处理?

发布于 10-20 07:30 字数 430 浏览 7 评论 0原文

尝试匹配元素并使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

筑梦2024-10-27 07:30:53

当作用域为 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/

满身野味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();
})

……当然,您需要适应您的确切需求。

Here’s an example of .each with a delay using jQuery’s .delay() function:

$('#show-articles').click(function(event) {
  var delayInterval = 1;
  $('.articles').each(function(){
    $(this).delay(delayInterval * 50).fadeIn()
    delayInterval++;
  })
  event.preventDefault();
})

…of course you’ll need to adapt to your exact need.

儭儭莪哋寶赑2024-10-27 07:30:53

您是否尝试过使用Jquery Delay

Have you tried using Jquery Delay?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文