3

我正在寻找在每三个 div 标签之后放置一个跨度标签的解决方案。

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>

需要

<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>

如您所见,结果有,跨度标记(<span class="clear"></span>),每三个 div 完成后,我尝试:

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>');
}

但没有得到预期的结果,
这里是链接:

http://jsfiddle.net/pxaS4/

请帮我解决。非常感谢。

4

3 回答 3

8

您可以使用:nth-child(3n)选择器:

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

演示:http: //jsfiddle.net/pxaS4/2/

于 2013-01-17T05:14:58.667 回答
1
$('.grid_5').each(function(index){

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

});
于 2013-01-17T05:14:50.193 回答
0
$(".grid_5").each(function (i) {
 if ((i+1)%3==0) $(this).after('<span class="clear"></span>');  
});
于 2013-01-17T05:16:24.803 回答