Nested jQuery.each() - continue/break

后端 未结 11 514
天涯浪人
天涯浪人 2020-12-05 03:52

Consider the following code:

    var sentences = [
        \'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\',
        \'Vivamus aliquet nisl quis          


        
11条回答
  •  生来不讨喜
    2020-12-05 04:28

    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 -1) {
                alert('found ' + words[w]);
                return;
            }
        }
    }
    

    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.

提交回复
热议问题