2012-04-02 2 views
4

を交互にCSSクラスを追加します。 2つの白い行、2つの赤い行、2つの白い行、2つの赤い行など。これはJQueryで可能ですか?は、私はそうのようなテーブルを持っているすべての2行

+0

この関連の質問を参照してください - http://stackoverflow.com/questions/3068480/how-can-i-add-a-class-to-を4th-1要素ごと –

+0

参照http://stackoverflow.com/questions/9794564/alternating-table-row-color-but-with-2rows-of-data –

答えて

4

http://jsbin.com/okahax/edit#javascript,html

var t = 0; 
$("table tr").each(function (i, n) 
{ 
    if (t < 2) 
    {$(this).css('background-color','red'); 

    } 
    else if (t < 4) 
    { 
     $(this).css('background-color','white'); 

    } 
    t++; 
    if (t==4) t=0; 
}) 
2

このような何か:

$('tr:nth-child(4n),tr:nth-child(4n-1)').addClass('alt'); 

これは、適切な式でnth-child selectorを使用しています。

デモ:http://jsfiddle.net/cnQNx/

または:

$('tr:nth-child(4n)').prev().andSelf().addClass('alt'); 
関連する問題