2012-11-01 75 views
6

我需要一個jquery腳本來將類動態添加到類行中的'div'。這門課是爲了設定利潤。如果在類行中有兩個div,只向第一個類添加類,並且如果有三個'div'添加類到兩個,則避免第三個。通過div循環動態添加類到兒童

因此,實際需要是計算'行'的div並將類添加到除最後一個之外的div。這裏是我的html: -

<div class="row"> 
        <div class="two-col"> 
         <h3>Header 2/3 Column</h3> 
         <p>Kidney Cancer Canada is a charitable patient-led support organization established to improve the quality of life for patients and their 
         families living with kidney cancer. <a href="#">Kidney Cancer Canada</a> advocates for access to netreatments, provides support and information to patients, 
         funds much-needed research, and works to increase awareness of kidney cancer as a significant health issue. Our goal is to help patients navigate 
         through information about their disease and ensure they have access to new treatment options available to them.</p> 
        </div> 
        <div class="one-col"> 
         <h3>Header 1/3 Column</h3> 
         <p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an 
         excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p> 
        </div> 
        <div class="clear"></div> 
       </div> 
       <div class="row"> 
        <div class="one-col"> 
         <h3>Header 1/3 Column</h3> 
         <p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an 
         excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p> 
        </div> 
        <div class="one-col"> 
         <h3>Header 1/3 Column</h3> 
         <p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an 
         excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p> 
        </div> 
        <div class="one-col"> 
         <h3>Header 1/3 Column</h3> 
         <p>KCC hosts patient and caregiver education meetings and webcasts from locations all across canada. Atttending meetings in-person provides an 
         excellent oppurtunity to meet other kidney cancer patients, caregivers, and healthcare professionals</p> 
        </div> 
        <div class="clear"></div> 
       </div> 
+0

別類* *實際需要動態增加或者這只是不知道如何應用CSS來的一切,但一個集合的最後一個孩子,所以你要訴諸的情況下, JS來破解它?如果它必須是動態的,則將該類應用於父級('.row')並通過'.row.newClass .one-col'或類似選擇器修改樣式會更有效。 – cimmanon

回答

4

如果你不希望這個類適用於您divsclear類,那麼你可以使用這個

$('div.row').find('div').addClass('yourclass').not(':last-child,.clear'); 

或者

$('div.row').find('div').addClass('yourclass').not(':last-child'); 

Live Sample

2

這裏是腳本來完成這個任務:

$.each($(".row"),function() { 

     var length = $(this).find('.two-col').length+$(this).find('.one-col').length+$(this).find('.three-col').length; 
      for(i=1;i<length;i++) 
      { 
      $(this).find(":nth-child("+i+")").addClass('margin-right'); 
      } 
    }); 
1
$("div.row").each(function(){ 

len = $(this).find("div").length ; 
if ($(this).find("div.clear").length > 0){ 
    len = len-1; 
} 
var i=0; 
$(this).find("div").each(function(){ 
if($(this).attr("class") != "clear"){ 
if (i < len-1){ 
    $(this).addClass("margin_class"); 
    } 
} 
i++; 

}); 

}); 
+0

在某些div中有

,這會導致不正確的輸出 –