Append Span Tag, after every 3rd div Jquery

冷暖自知 提交于 2019-12-23 16:34:44

问题


i am looking for the solution to place a span tag, after every third div tag .

HTML

<div class="grid_15">
   <div class="grid_5">
      <p>Sun</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Mon</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Tue</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Wed</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Thu</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Fri</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Sat</p>
      <div>
         <div class="suggest nudge-up">
            <div class="form-content-left">Morning</div>
            <div class="form-content-right">(7.00 - 8.00)</div>
            <span class="clear"></span><span class="clear"></span>
         </div>
      </div>
   </div>
</div>

Needed

<div class="grid_15">
   <div class="grid_5">
      <p>Sun</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Mon</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Tue</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   **<span class="clear"></span>**
   <div class="grid_5">
      <p>Wed</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Thu</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   <div class="grid_5">
      <p>Fri</p>
      <div>
         <div class="suggest nudge-up"><span class="form-content-left">All Hours</span><span class="clear"></span></div>
      </div>
   </div>
   **<span class="clear"></span>**
   <div class="grid_5">
      <p>Sat</p>
      <div>
         <div class="suggest nudge-up">
            <div class="form-content-left">Morning</div>
            <div class="form-content-right">(7.00 - 8.00)</div>
            <span class="clear"></span><span class="clear"></span>
         </div>
      </div>
   </div>
</div>

As you can See, the result has, span tag (<span class="clear"></span>), after every third div completion, I tried :

jQuery

var a = $('html').find('div.grid_15 > div.grid_5');
for( var i = 0; i < a.length; i+=3 ) {
a.slice(i, i+3).append('<span class="clear"></span>');
}

But not getting the result as expected,
here is the link :

http://jsfiddle.net/pxaS4/

please help me with solution. Thanks a lot.


回答1:


You can use the :nth-child(3n) selector:

$('div.grid_15 > div.grid_5:nth-child(3n)').after('<span class="clear">foo</span>');

Demo: http://jsfiddle.net/pxaS4/2/




回答2:


$('.grid_5').each(function(index){

if(((index+1)%3)==0)
{
$(this).after('<span class="clear"></span>');
}

});



回答3:


$(".grid_5").each(function (i) {
 if ((i+1)%3==0) $(this).after('<span class="clear"></span>');  
});


来源:https://stackoverflow.com/questions/14372545/append-span-tag-after-every-3rd-div-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!