【问题标题】:jQuery loop without each and callback function没有每个和回调函数的jQuery循环
【发布时间】:2014-01-16 23:45:56
【问题描述】:

我希望在没有each 和回调调用的情况下循环抛出 jQuery 集合。

我有以下代码

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

if(found1){
    return;
}

一旦found1 设置为真,下次它总是为真。我想知道如何在没有each 和回调的情况下循环

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

更新

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

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

http://jsfiddle.net/LwTGU/

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

【问题讨论】:

    标签: javascript jquery loops


    【解决方案1】:

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

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

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

    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 输出是您所期望的。

    那么,回到你原来的问题。听起来您只需要在找到匹配项后跳出循环。如果您想知道如何跳出 jQuery each 循环,只需在设置 found1 = true; 后使用 return false;。你不应该害怕传递回调;该回调只是在常规的旧 for 循环“幕后”中为选择器中的每个元素执行。

    如果您真的想自己编写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
        }
    }
    

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

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

    除非选择器只返回少数元素(例如

    【讨论】:

    • 看起来不像 jQuery 代码通常那样神奇。但是我花了一些时间在谷歌上搜索,我认为这是我能得到的最好的代码。谢谢。
    • @Max:我刚刚看到了对小提琴示例的问题的编辑。我已经在我的回答中添加了一些关于您的示例的附加信息以及您要检查的小提琴的修订版。
    【解决方案2】:

    听起来你想在满足某些条件时停止处理。您可以通过在满足条件时返回false 来使用each:

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

    您也可以像其他数组一样迭代$('#Root div.ListItem') 的返回值(如果您坚持不使用each)。

    【讨论】:

      【解决方案3】:

      由于我没有必要的声誉 (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'));
      }
      

      除非我遗漏了什么,否则“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'));
      }
      

      【讨论】:

        猜你喜欢
        • 2016-12-21
        • 2015-11-23
        • 2016-01-21
        • 1970-01-01
        • 2013-01-18
        • 2013-12-11
        • 2017-06-02
        • 2018-04-25
        • 2015-07-16
        相关资源
        最近更新 更多