【发布时间】:2015-01-21 00:31:42
【问题描述】:
有没有办法将第一个选项“选择”的颜色更改为红色,将其余选项更改为绿色?
到目前为止,toggleClass 允许我在颜色之间进行更改,但我希望每当用户单击“选择”时颜色变为红色,而每当有人单击其余选项时颜色变为绿色(即“服务一,服务-两个”等)。
HTML:
<div class="field">
<label>Service</label>
<select id="select">
<option>Select</option>
<optgroup label="Type of Service">
<option>Service-One</option>
<option>Service-Two</option>
</optgroup>
<optgroup label="Type of Service">
<option>Service-Three</option>
<option>Service-Four</option>
</optgroup>
</select>
</div>
这是我的 jQuery:
$("select").each(function(){
$(this).toggleClass('success-select');
});
这是我的 codePen:http://codepen.io/johnnyginbound/pen/MYmaaz
【问题讨论】: