2014-01-12 12 views
0

我試圖使一對或不成對的div從容器有不同的背景。例如,第一個具有紅色背景,第二個藍,紅三第四藍色等...只從一個容器更改一對或不成對div

這是我的代碼:

<div>...</div> 
<div>...</div> 
<div>...</div> 
<div>...</div> 
<div>...</div> 
<div>...</div> 

div { width:50px; height:50px; background:red; } 

我真的不知道該如何實現這個。我不知道我是否只需要css或jquery。

謝謝。

回答

1

您可以通過使用:nth-child選擇只用CSS做:

div { width: 50px; height: 50px; } 
div:nth-child(odd) { background-color: red; } 
div:nth-child(even) { background-color: blue; } 

jsfiddle

0

使用僞選擇器的CSS屬性的第n個孩子。通過使用這個ü可以選擇奇數偶節點

div:nth-child(odd) { background-color: #FF0000; } 
div:nth-child(even) { background-color: #0000FF; } 
相關問題