jQuery循环没有每个和回调函数

时间:2022-12-08 19:57:40

I wish to loop throw jQuery collection without each and callback call.

我希望循环抛出jQuery集合,而不需要每个和回调调用。

I have following code

我有以下代码

var found1 = false;
$('#Root div.ListItem').each(function( index, d1 ) { 
    if (group == d1.text()){ found1 = true; }
} );

if(found1){
    return;
}

Once found1 is set true Next time it is always true. I would like to know how to loop without each and callback like

一旦找到found1设置为true,则下次始终为true。我想知道如何循环没有每个和回调像

for(var id in $('#Root div.ListItem')) { ... }

UPDATE

UPDATE

I am not wondering how to break loop. I do not wish to pass callback in each

我不知道如何打破循环。我不想在每个回调中传递回调

If I pass jQuery object in loop then I get to many keys.

如果我在循环中传递jQuery对象,那么我会得到很多键。

http://jsfiddle.net/LwTGU/

http://jsfiddle.net/LwTGU/

In that example there might be 1 key for one child element.

在该示例中,一个子元素可能有1个键。

3 个解决方案

#1


7  

Your attempt to use a for-in statement in your fiddle is actually iterating over all of the properties in the jQuery array-like object:

您在小提琴中使用for-in语句的尝试实际上是迭代jQuery数组类对象中的所有属性:

for (var id in $('#Root div.ListItem')) {
    // id is the name of the property
}

You don't want this; you need to iterate over the elements in the array-like object:

你不想要这个;你需要遍历类数组对象中的元素:

for (var id in $('#root span').toArray()) { // convert to native array
    // id is now an element found by the selector
    $('<div />', {text: $(id).text()}).appendTo($('#out'));
}

You'll see in the above that the output is what you were expecting.

您将在上面看到输出是您所期望的。

So, back to your original question. It sounds like you just need to break out of your loop once you've found a match. In case you're wondering how to break out of a jQuery each loop, simply return false; after you set found1 = true;. You shouldn't be afraid to pass in a callback; that callback is just executed for each element in your selector in a regular old for-loop "under-the-hood."

所以,回到你原来的问题。听起来你只需要在找到匹配后突破你的循环。如果你想知道如何在每个循环中打破jQuery,只需返回false;设置found1 = true后。你不应该害怕传递回调;该回调仅针对选择器中的每个元素在常规的旧for循环“引擎盖下”执行。

If you really want to write the for-each structure yourself, then something like this would be sufficient:

如果你真的想自己编写for-each结构,那么这样就足够了:

var found1 = false;
var items = $('#Root div.ListItem').toArray(); // an array of DOM elements
for (var i = 0, j = items.length; i < j; i++) {
    // You can access the DOM elements in the array by index, but you'll
    // have to rewrap them in a jQuery object to be able to call .text()
    if (group == $(items[i]).text()) {
        found1 = true; 
        break; // no need to keep iterating once a match is found
    }
}

A shorter but slower way to do this might be to use $.grep and check if it found anything:

一个更短但更慢的方法可能是使用$ .grep并检查它是否找到了任何东西:

var found1 = $.grep($('#Root div.ListItem'), function() {
    return group == $(this).text();
}).length > 0;

I wouldn't recommend the latter unless the selector is only returning a handful of elements (e.g. < 50).

除非选择器只返回少量元素(例如<50),否则我不建议使用后者。

#2


3  

It sounds like you want to stop processing when you meet some condition. You can do that with each by returning false when the condition is met:

听起来你想要在遇到某些条件时停止处理。您可以通过在满足条件时返回false来执行此操作:

var found1 = false;
$('#Root div.ListItem').each(function(index, d1) { 
    if (group == d1.text()) { 
        found1 = true; 
        return false;
    }
});

You can also iterate over the return value of $('#Root div.ListItem') like it's any other array (if you insist on not using each).

您还可以迭代$('#Ring div.ListItem')的返回值,就像它的任何其他数组一样(如果您坚持不使用每个数组)。

#3


2  

As I do not have the necessary reputation (50), I was not able to comment on the accepted answer. However, I do not believe that the following code will work as expected:

由于我没有必要的声誉(50),我无法对接受的答案发表评论。但是,我不相信以下代码将按预期工作:

for (var id in $('#root span').toArray()) { // convert to native array
    // id is now an element found by the selector
    $('<div />', {text: $(id).text()}).appendTo($('#out'));
}

Unless I'm missing something, "id" would be an integer representing the iterated array index, not the actual array element. If so, $(id) would not point to a valid jquery object. Instead, wouldn't it need to be something like this?

除非我遗漏了某些内容,否则“id”将是表示迭代数组索引的整数,而不是实际的数组元素。如果是这样,$(id)将不会指向有效的jquery对象。相反,它不需要是这样的吗?

for (var id in $('#root span').toArray()) { // convert to native array
    // id is now an element found by the selector
    $('<div />', {text: $($('#root span')[id]).text()}).appendTo($('#out'));
}

#1


7  

Your attempt to use a for-in statement in your fiddle is actually iterating over all of the properties in the jQuery array-like object:

您在小提琴中使用for-in语句的尝试实际上是迭代jQuery数组类对象中的所有属性:

for (var id in $('#Root div.ListItem')) {
    // id is the name of the property
}

You don't want this; you need to iterate over the elements in the array-like object:

你不想要这个;你需要遍历类数组对象中的元素:

for (var id in $('#root span').toArray()) { // convert to native array
    // id is now an element found by the selector
    $('<div />', {text: $(id).text()}).appendTo($('#out'));
}

You'll see in the above that the output is what you were expecting.

您将在上面看到输出是您所期望的。

So, back to your original question. It sounds like you just need to break out of your loop once you've found a match. In case you're wondering how to break out of a jQuery each loop, simply return false; after you set found1 = true;. You shouldn't be afraid to pass in a callback; that callback is just executed for each element in your selector in a regular old for-loop "under-the-hood."

所以,回到你原来的问题。听起来你只需要在找到匹配后突破你的循环。如果你想知道如何在每个循环中打破jQuery,只需返回false;设置found1 = true后。你不应该害怕传递回调;该回调仅针对选择器中的每个元素在常规的旧for循环“引擎盖下”执行。

If you really want to write the for-each structure yourself, then something like this would be sufficient:

如果你真的想自己编写for-each结构,那么这样就足够了:

var found1 = false;
var items = $('#Root div.ListItem').toArray(); // an array of DOM elements
for (var i = 0, j = items.length; i < j; i++) {
    // You can access the DOM elements in the array by index, but you'll
    // have to rewrap them in a jQuery object to be able to call .text()
    if (group == $(items[i]).text()) {
        found1 = true; 
        break; // no need to keep iterating once a match is found
    }
}

A shorter but slower way to do this might be to use $.grep and check if it found anything:

一个更短但更慢的方法可能是使用$ .grep并检查它是否找到了任何东西:

var found1 = $.grep($('#Root div.ListItem'), function() {
    return group == $(this).text();
}).length > 0;

I wouldn't recommend the latter unless the selector is only returning a handful of elements (e.g. < 50).

除非选择器只返回少量元素(例如<50),否则我不建议使用后者。

#2


3  

It sounds like you want to stop processing when you meet some condition. You can do that with each by returning false when the condition is met:

听起来你想要在遇到某些条件时停止处理。您可以通过在满足条件时返回false来执行此操作:

var found1 = false;
$('#Root div.ListItem').each(function(index, d1) { 
    if (group == d1.text()) { 
        found1 = true; 
        return false;
    }
});

You can also iterate over the return value of $('#Root div.ListItem') like it's any other array (if you insist on not using each).

您还可以迭代$('#Ring div.ListItem')的返回值,就像它的任何其他数组一样(如果您坚持不使用每个数组)。

#3


2  

As I do not have the necessary reputation (50), I was not able to comment on the accepted answer. However, I do not believe that the following code will work as expected:

由于我没有必要的声誉(50),我无法对接受的答案发表评论。但是,我不相信以下代码将按预期工作:

for (var id in $('#root span').toArray()) { // convert to native array
    // id is now an element found by the selector
    $('<div />', {text: $(id).text()}).appendTo($('#out'));
}

Unless I'm missing something, "id" would be an integer representing the iterated array index, not the actual array element. If so, $(id) would not point to a valid jquery object. Instead, wouldn't it need to be something like this?

除非我遗漏了某些内容,否则“id”将是表示迭代数组索引的整数,而不是实际的数组元素。如果是这样,$(id)将不会指向有效的jquery对象。相反,它不需要是这样的吗?

for (var id in $('#root span').toArray()) { // convert to native array
    // id is now an element found by the selector
    $('<div />', {text: $($('#root span')[id]).text()}).appendTo($('#out'));
}