嵌套jQuery.each()-继续/中断 [英] Nested jQuery.each() - continue/break

查看:229
本文介绍了嵌套jQuery.each()-继续/中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

    var sentences = [
        'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
        'Vivamus aliquet nisl quis velit ornare tempor.',
        'Cras sit amet neque ante, eu ultrices est.',
        'Integer id lectus id nunc venenatis gravida nec eget dolor.',
        'Suspendisse imperdiet turpis ut justo ultricies a aliquet tortor ultrices.'
    ];

    var words = ['ipsum', 'amet', 'elit'];

    $(sentences).each(function() {
        var s = this;
        alert(s);
        $(words).each(function(i) {
            if (s.indexOf(this) > -1)
            {
                alert('found ' + this);
                return false;
            }
        });
    });

有趣的部分是嵌套的jQuery.each()循环.根据文档,返回false将退出循环(停止执行循环-类似于普通的JavaScript break语句),返回non-false将停止当前迭代并继续下一次迭代(类似于普通的JavaScript Continue语句).

The interesting part is the nested jQuery.each() loops. As per the documentation, returning false will break out of the loop (discontinuing execution of the loop - similar to a normal JavaScript break statement), and returning non-false will stop the current iteration and continue with the next iteration (similar to a normal JavaScript continue statement).

我可以自行中断或继续jQuery.each(),但是使用嵌套的jQuery.each,我发现很难从子循环中脱离父循环.我可以使用布尔值,并在每次子迭代时更新它,但是我想知道是否有更简单的方法.

I can break or continue a jQuery.each() on its own, but with nested jQuery.each, I've found it difficult to break out of the parent loop from within the child loop. I could use a boolean value, and update it on every child iteration, but I was wondering if there was an easier way.

如果您想弄乱它,我已经在jsFiddle上设置了一个示例.只需单击测试"按钮即可运行上面显示的示例.

I've set up an example at jsFiddle if you'd like to mess around with it. Simply click the "Test" button to run the example shown above.

TLDR:在jQuery的上下文中,是否有任何类似于标记为继续或中断的内容?

TLDR: Is there anything resembling a labeled continue or break within the context of jQuery?

推荐答案

您应该在没有jQuery的情况下执行 ,它可能不像漂亮",但是执行的次数更少,并且精确地执行起来更容易您想要什么,像这样:

You should do this without jQuery, it may not be as "pretty" but there's less going on and it's easier to do exactly what you want, like this:

var sentences = [
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
    'Vivamus aliquet nisl quis velit ornare tempor.',
    'Cras sit amet neque ante, eu ultrices est.',
    'Integer id lectus id nunc venenatis gravida nec eget dolor.',
    'Suspendisse imperdiet turpis ut justo ultricies a aliquet tortor ultrices.'
];

var words = ['ipsum', 'amet', 'elit'];

for(var s=0; s<sentences.length; s++) {
    alert(sentences[s]);
    for(var w=0; w<words.length; w++) {
        if(sentences[s].indexOf(words[w]) > -1) {
            alert('found ' + words[w]);
            return;
        }
    }
}

您可以在此处尝试.我不确定这是否是您要执行的 exact 行为,但是现在您不在双

You can try it out here. I'm not sure if this is the exact behavior you're after, but now you're not in a closure inside a closure created by the double .each() and you can return or break whenever you want in this case.

这篇关于嵌套jQuery.each()-继续/中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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