如何在不同的div中追加循环值

时间:2015-10-09 07:57:34

标签: javascript jquery

我需要追加不同div - s

的值
var thmb_count = thmb_wrap.length;

for( i = 0; i < thmb_count; i++){
    var num = i + 1; 
    thmb_wrap.each(function(){
        $(this).append('<span class="numeric">' + num + '/' + (full) + '</span>');
    });
}

html

<div class="thumb-wrp scrollbar-inner">
    <div class="thm-img"><img src="img/img/a.jpg"></div>
    <div class="thm-img"><img src="img/img/b.jpg"></div>
    <div class="thm-img"><img src="img/img/c.jpg"></div>
    <div class="thm-img"><img src="img/img/d.jpg"></div>
    <div class="thm-img"><img src="img/img/a.jpg"></div>
    <div class="thm-img"><img src="img/img/c.jpg"></div>
    <div class="thm-img"><img src="img/img/b.jpg"></div>
    <div class="thm-img"><img src="img/img/d.jpg"></div>
</div>

1 个答案:

答案 0 :(得分:2)

您可以迭代thm-img并附加范围

&#13;
&#13;
var $thumbs = $('.thumb-wrp .thm-img'),
  full = $thumbs.length;
$thumbs.append(function(i) {
  return '<span class="numeric">' + (i + 1) + '/' + (full) + '</span>'
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="thumb-wrp scrollbar-inner">
  <div class="thm-img"><img src="img/img/a.jpg"></div>
  <div class="thm-img"><img src="img/img/b.jpg"></div>
  <div class="thm-img"><img src="img/img/c.jpg"></div>
  <div class="thm-img"><img src="img/img/d.jpg"></div>
  <div class="thm-img"><img src="img/img/a.jpg"></div>
  <div class="thm-img"><img src="img/img/c.jpg"></div>
  <div class="thm-img"><img src="img/img/b.jpg"></div>
  <div class="thm-img"><img src="img/img/d.jpg"></div>
</div>
&#13;
&#13;
&#13;