【问题标题】:Get index of element in array jQuery获取数组jQuery中元素的索引
【发布时间】:2014-12-15 08:29:55
【问题描述】:

我有一个对象数组$('selector'),我想获取每个元素的index() 并将其附加到每个元素的html 中。我可以获取第一个元素的索引,只需编写$('selector').index(),但我如何获取其他元素,并将它们附加到它们的 DOM 对象。帮助,我是 Javascript 和 jQuery 的新手。

【问题讨论】:

  • 你可以使用.each()
  • 谢谢你们,感谢你们的帮助)

标签: javascript jquery object dom


【解决方案1】:

您可以将函数传递给.html() jQuery 方法。这个函数会很方便地用两个参数调用,元素索引和当前 HTML,所以你想要的很简单:

$elements.html(function(i, v){
   return v + i; 
});

JSFiddle

这将为您提供相对于所做选择的index。如果你想要相对于每个元素兄弟的索引,你需要.index():

$('ul li').html(function(_, v){
   return v + $(this).index();
});

JSFiddle

【讨论】:

  • 非常好。感谢乔治的“新事物”:)
  • @Archer 很高兴你这么认为。事实上,许多 jQuery 方法都支持将函数传递给它们的 setter 版本。
  • 是的,我知道这一点,但从来没有想到过。我还没有看到它以这种特殊的方式使用过,但我喜欢它!
【解决方案2】:

你也可以使用 eq:

$('selector').each(function(){
  $(this).html('index for this html is ' + $(this).eq());
});

【讨论】:

    【解决方案3】:

    试试:

    $.each($('selector'), function() {
        $(this)append($(this).index());
    });
    

    【讨论】:

      【解决方案4】:

      只需循环遍历它们,迭代器就会告诉你它是索引。

      var array = $('.someClass');
      for(var i=0; i<array.length; i++){
          console.log('Element #' + i + ': ' + $(array[i]).text());
      }
      

      【讨论】:

        【解决方案5】:

        你应该使用.each进行迭代

        示例代码:

        $("selector").each(function(i, v) {
          $(this).append(i);
        });
        

        【讨论】:

          【解决方案6】:

          使用 .each() 选择每个元素,获取每个索引并将每个索引附加到与选择器匹配的所有元素。

          $("selector").each(function(index) {
          
             var eachIndex = $(this).index();
          
             $(this).append(eachIndex);
          
          });
          

          【讨论】:

            【解决方案7】:

            使用JQuery.each:

            $('selector').each(function(index) {
              console.log(index);
            });
            

            示例:

            HTML:

            <div>My Div</div>
            <div>My Div</div>
            <div>My Div</div>
            <div>My Div</div>
            <div>My Div</div>
            <div>My Div</div>
            <div>My Div</div>
            <div>My Div</div>
            <div>My Div</div>
            

            JS:

            $('div').each(function(index){
                var newHtml = $(this).html() + " - Index:" + index;
                $(this).html(newHtml);
            });
            

            输出:

            My Div - Index:0
            My Div - Index:1
            My Div - Index:2
            My Div - Index:3
            My Div - Index:4
            My Div - Index:5
            My Div - Index:6
            My Div - Index:7
            My Div - Index:8
            

            JSFiddle.

            【讨论】:

              【解决方案8】:

              试试这个...

              $("selector").each(function(i) {
                  $(this).html($(this).html() + " " + i);
              });
              

              i 将是每个选定元素的索引,并将附加到 html。

              这是一个 jsfiddle 示例...

              http://jsfiddle.net/54bcn68j/

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-09-06
                • 2012-04-10
                • 1970-01-01
                • 1970-01-01
                • 2011-12-14
                • 2011-04-15
                相关资源
                最近更新 更多