如何动态退出jQuery $ .each()? [英] how to dynamically exit a jquery $.each()?

查看:75
本文介绍了如何动态退出jQuery $ .each()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像列表,这些图像是我通过ajax获取的,然后使用jquery $ .each()遍历图像,并在一秒钟的间隔后一个接一个地显示图像. 我希望用户能够单击停止按钮,以便用户可以在特定图像处停止. 因此,当用户单击停止按钮时,我需要动态退出$ .each(). 有可能做到吗?

i have a list of images which i am getting through ajax and then using jquery $.each() i loop through the images and display a image one after the other after an interval of one second. I want the user to be able click on a stop button and so that the user can stop at a particular image if he wants to. So i need to dynamically exit $.each() when the user clicks on the stop button. Is it possible to do it?

推荐答案

您可以使用return false尽早摆脱each()循环.

You can use return false to break out of each() loops early.

<script>
    $("button").click(function () {
      $("div").each(function (index, domEle) {
        // domEle == this
        $(domEle).css("backgroundColor", "yellow"); 
        if ($(this).is("#stop")) {
          $("span").text("Stopped at div index #" + index);
          return false;
        }
      });
    });

</script>

来源: http://api.jquery.com/each/

这篇关于如何动态退出jQuery $ .each()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆