2013-03-30 47 views
-1

我有一個網站,我要將元素作爲瓷磚排成4行。我想添加到每個元素margin-left: 13px,除了每行中的每個第一個元素。任何人都可以請我指出如何使用jQuery完成這項工作?如何添加類,使用jQuery,除了每四分之一的每個元素?

+3

請發表您的HTML的一個例子。沒有它,所有的答案只能是猜測。 –

+0

標題是關於元素1,2,3,5,6,7,9,...,而身體是關於元素2,3,4,6,7,8,10 ... –

回答

3

工作例如:http://jsfiddle.net/ZVd6Y/

假設包裝行有一類row ...

和給定HTML :

<div class="row"> 
    <p class="first">first row</p> 
    <p>second row</p> 
    <p>third row</p> 
</div> 

<div class="row"> 
    <p class="first">first row</p> 
    <p>second row</p> 
    <p>third row</p> 
</div> 

您可能需要使用:

$(document).ready(function(){ 
    $('.row').children("p:not(.first)").addClass("extra-margin"); 
}); 

用下面CSS:

.extra-margin{ 
    margin-left: 13px; 
} 
0

試試這個:

var count = 0; 
    $('.classname').each(function(){ 
     count++; 
     if(count !== 4){ 
      $(this).css("margin-left", "13px"); 
     } else { 
      count = 0; 
     } 
    }); 
相關問題