【问题标题】:how to get children array of an element in jquery如何在jquery中获取元素的子数组
【发布时间】:2012-02-27 10:23:46
【问题描述】:

这是我的元素,我想通过循环排列其中的孩子。

<div id="animDummy1">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

这就是我想象的代码应该看起来的样子,但是 children(),当然,不会返回一个孩子的数组

var children=$('#animDummy1').children();
for(var i=0;i<children.length;i++){
    children[i].css('left',i*120+'px');
}

问题 - 我可以让 children 数组在循环中使用吗?我知道我可以为每个要执行的孩子附加一个函数,但是我可以在那里得到那个增加的“i”吗?

谢谢。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    children() 返回一个类似于 DOM 节点数组的子项的 jQuery 对象。您的问题在循环内部 - 当您使用 [] 访问单个对象时,您会返回没有 css 方法的普通 DOM 节点。使用.eq(i)$(children[i])

    或者只使用each() 方法,它可以让您做同样的事情,而无需手动编写for 循环。阅读文档以了解如何获取回调中元素的索引。

    【讨论】:

    • 这不太正确 - children() 返回一个 jQuery 对象而不是一个数组。它确实有 jQuery.each() 可以使用的“索引”,但它不是数组类型。如果您想获得一个实际的数组,请尝试使用children().toArray()。要确认这一点,请尝试 children() instance of Arraychildren().toArray() instanceof Array :)
    • @Jason:好吧,我猜我的语言不是 100% 准确的。
    【解决方案2】:

    这是正确的方法。

    var children=$('#animDummy1').children();
    
    children.each(function(idx, val){
       $(this).css('left',idx*120+'px');
    });
    

    或者实际上这样更好。

    $('#animDummy1').children().each(function(idx, val){
       $(this).css('left',idx*120+'px');
    })
    

    【讨论】:

    • “i”在哪里被定义/增加?
    • 需要定义i变量。应该是function(i),或者在function中定义。
    【解决方案3】:

    children() 返回一组 jQuery 对象,children[i(anyNumber)] 将返回 dom 元素。所以在dom元素上调用cssjQuery方法会报错。如果要访问给定索引处的任何特定元素,则应使用 eq 方法。试试这个。

    var children = $('#animDummy1').children();
    for(var i = 0;i < children.length;i++){
        children.eq(i).css('left', (i*120+'px') );
    }
    

    .eq()参考:http://api.jquery.com/eq/

    【讨论】:

      【解决方案4】:

      许多 jQuery 方法允许您直接传递一个函数来代替您要分配的新值。

      举个例子……

      $('#animDummy1').children().css('left', function(i, curr_left) {
          return i * 120;
      });
      

      所以这里我直接在.children() 集合上调用.css(),而不是'left' 值的数字,我给出了一个函数。

      该函数为每个元素调用一次。参数表示当前迭代的索引,以及迭代中当前元素的当前css值。

      函数的return 值将用作当前元素上设置的值。

      【讨论】:

      • 这很甜蜜。我不知道我可以那样做!
      • @thenetimp:是的,这很方便。相当多的 jQuery 函数都有这个选项。
      【解决方案5】:

      使用 jQuery 的 children().each() 的其他答案可能对大多数情况都有帮助。但是,如果您确实需要一个实际的 javascript 数组,您可以执行以下操作:

      var childrenArray = $('#animDummy1').children().toArray();
      

      这将允许您在循环中使用真正的数组或与需要数组作为参数的其他函数一起使用。

      【讨论】:

      • 对我只需要 JS 数组与 concat() 一起使用的情况最有帮助的答案
      【解决方案6】:

      这对我来说很好用

      $(document).ready(function(){
      
          var children = $('#animDummy1').children();        
      
          $(children).each(function(index, item){                 
              console.log(index);            
          });           
      });
      

      jsFiddle Example

      【讨论】:

        【解决方案7】:

        使用 jQuery:

        .each() 获取子元素为 $('#someObject') 的数组的每个索引:

        $('#animDummy1').each(function(index, item) {
           // Do Something with each $(item) children of #animDummy with index
        });
        

        【讨论】:

        • 请详细说明你的答案。
        【解决方案8】:

        轻轻一点,代码就可以工作了。

        var children=$('#animDummy1').children().toArray();
        
        var children=$('#animDummy1').children();
        for(var i=0;i<children.length;i++){
            $(children[i]).css('left',i*120+'px');
        }
        

        您的代码没问题,除了 $(children[i]) 并使用 .toArray()

        将孩子定义为数组

        【讨论】:

          【解决方案9】:
              $(function () {
                  var sortOrder = [3, 4, 1, 5,2];
                  var onLoad = $('#parentDiv>p').get();
                  for (var i = 0; i < onLoad.length; i++) {
                      var inRearranging = $('#parentDiv>P').get();
                      var orderingID = "#paragraph" + sortOrder[i];
                      $(orderingID).insertBefore("#" + inRearranging[i].id);
                  }
              });
          
          
          
          <div id="parentDiv">
              <p id="paragraph1">1</p>
              <p id="paragraph2">2</p>
              <p id="paragraph3">3</p>
              <p id="paragraph4">4</p>
              <p id="paragraph5">5</p>
          </div>
          

          【讨论】:

            猜你喜欢
            • 2019-12-17
            • 2012-01-31
            • 2011-09-06
            • 2013-06-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多