把每个小组的eq()放在标题中

时间:2016-03-16 06:55:06

标签: javascript jquery

.scroll

如果它是面板标题中的第1个2'和3'rd ...面板,则应该放置以下内容

<div class="panel panel-default">
  <div class="panel-heading">--Here should say 1--</div>
  <div class="panel-body">
    Panel content
  </div>
</div>

<div class="panel panel-default">
  <div class="panel-heading">--Here should say 2--</div>
  <div class="panel-body">
    Panel content
  </div>
</div>

3 个答案:

答案 0 :(得分:3)

您需要在面板标题列表中使用jquery index()以及$(".panel .panel-heading").each(function(){ $(this).append($(this).index('.panel-heading') + 1); });函数和面板标题元素。

元素索引从0开始,因此您需要向其添加1才能查看正确的编号:

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="panel panel-default">
  <div class="panel-heading"></div>
  <div class="panel-body">
    Panel content
  </div>
</div>

<div class="panel panel-default">
  <div class="panel-heading"></div>
  <div class="panel-body">
    Panel content
  </div>
</div>
&#13;
    #container
    {
    position: absolute;
    overflow-x: hidden;
    overflow-y:auto;
    width: 100%;
    height: 100%;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    margin: auto; 
   }
&#13;
&#13;
&#13;

答案 1 :(得分:3)

使用.html()后跟下面的函数:

$('.panel-heading').html( function ( i, old ) {
   return ( i + 1 );
});

DEMO

您是否完全理解这句话的含义the following should put if its the 1'st 2'nd 3'rd,您是否只想将panel元素1st, 2nd and 3rd的内容放入其中甚至存在更多?如果是这样的话:

$('.panel-heading').html( function ( i, old ) { 
  if ( i < 3 )  return ( i + 1 );  
});

答案 2 :(得分:1)

$('.panel .panel-heading').each(function(k,v){ $(v).text(k); });
// or if you want to start from 1:
$('.panel .panel-heading').each(function(k,v){ $(v).text(k + 1); });