div中数字的jQuery增量

时间:2017-10-14 09:27:48

标签: javascript jquery html

我想在div中使用jQuery

增加+1

原始代码如下所示:

<div class="timeline">
<div class="timeline__item">
    <div class="timeline__item__icon-wrap">
         <div class="timeline__item__icon">
             1
         </div>
    </div>
    <div class="timeline__item__content padding-left">
         <div class="timeline__item__description">
              <h3>Day 1</h3>
             <p>t   t  t  t  t  t  t  t  t  t</p>
         </div>
    </div>
</div>
<div class="timeline__item">
     <div class="timeline__item__icon-wrap">
         <div class="timeline__item__icon">
             1
         </div>
     </div>
    <div class="timeline__item__content padding-left">
         <div class="timeline__item__description">
             <h3>Day 2</h3>
             <p>t   t  t  t  t  t  t  t  t  t</p>
         </div>
     </div>
 </div>
 <div class="timeline__item">
     <div class="timeline__item__icon-wrap">
         <div class="timeline__item__icon">
              1
         </div>
     </div>
     <div class="timeline__item__content padding-left">
         <div class="timeline__item__description">
             <h3>Day 3</h3>
             <p>t   t  t  t  t  t  t  t  t  t</p>
         </div>
     </div>
 </div>  

.timeline__item__icon中的那些1应该返回1,2,3 ......

我试过

jQuery( document ).ready(function() {
var number = 1;
 jQuery( ".timeline__item__icon-wrap > div > div" ).replaceWith( number );
return ++number;
});

1 个答案:

答案 0 :(得分:2)

$('.timeline__item__icon').each((i, e) => $(e).text(i+1));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="timeline__item__icon">
  1
</div>
<div class="timeline__item__icon">
  1
</div>
<div class="timeline__item__icon">
  1
</div>