2012-11-25 98 views
0

我有一組四種顏色,我想知道是否有可能使用這四種顏色作爲循環來改變模式中的div的顏色。因此,舉例來說:不同的背景顏色在一種模式中使用四種顏色

DIV1 =紅色 DIV2 =藍色 DIV3 =綠色 DIV4 =黃色 [重新開始] DIV5 =紅色 DIV2 =藍色 DIV3 =綠色...等等。

我在想jQuery/JS會是最好的。我嘗試使用第n個孩子等,但沒有那麼強大。

任何幫助?

感謝, [R

回答

3

你可以只用CSS做:

div:nth-child(4n+1) { background-color : red; } 
div:nth-child(4n+2) { background-color : blue; } 
div:nth-child(4n+3) { background-color : green; } 
div:nth-child(4n+4) { background-color : yellow; } 

演示:http://jsfiddle.net/RLmgD/

但是,如果你真的想要的jQuery:

$(document).ready(function() { 
    $("div:nth-child(4n+1)").css("background-color","red"); 
    $("div:nth-child(4n+2)").css("background-color","blue"); 
    $("div:nth-child(4n+3)").css("background-color","green"); 
    $("div:nth-child(4n+4)").css("background-color","yellow"); 
}); 

演示: http://jsfiddle.net/RLmgD/1/

+0

+1好的豐富多彩的解決方案。 – Adil

+0

@Adil - 這不是多彩。 [_This_](http://jsfiddle.net/RLmgD/3/)很多彩... – nnnnnn

+0

是的,看起來不錯。我希望我可以放棄更多的流行音樂和精彩。 – Adil