2013-05-07 128 views
0

我的選擇列表是從我的數據庫動態生成的。我想有每個項目的下拉備用背景色像本實施例中(從here):交替選擇列表選項顏色

lists

因此,基本上,我需要交替類ofeach選項標籤,如在下面的代碼塊。我如何使用jQuery來做這件事,因爲我無法手動在自己寫顏色?

<FORM> 
    <SELECT> 
     <OPTION>What is your preferred browser?</OPTION> 
     <OPTION class="mar">Explorer 5.5</OPTION> 
     <OPTION class="white">Explorer 6.0</OPTION> 
     <OPTION class="mar">Netscape 4.7</OPTION> 
     <OPTION class="white">Netscape 6.0</OPTION> 
     <OPTION class="mar">WebTV</OPTION> 
     <OPTION class="white">Lynx</OPTION> 
     <OPTION class="mar">Other</OPTION> 
    </SELECT> 
</FORM> 

回答

2

試試這個:

$('select option:even').css({'background-color': 'red'}) 

FIDDLE DEMO

1

您可以使用:even:odd選擇:

$('#selectId option:even').addClass('class1'); 
$('#selectId option:odd').addClass('class2'); 
0
option:nth-child(even){background-color:white;} 

你不東東d(來源)的jQuery庫。