3

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>)があり、3回の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 に答える