以下代码在除 Internet Explorer 9 之外的所有其他浏览器中都能完美运行。彩色透明 CSS 不起作用。
HTML:
<select class="selectElement" runat="server" id="dropdown_">
<option value="N">N</option>
<option value="G">G</option>
<option value="O">O</option>
<option value="A">A</option>
<option value="R">R</option>
<option value="U">U</option>
</select>
CSS:
.selectElement {
height: 50px;
width: 80px;
border: solid 1px #c8c8c8;
color:transparent;
}
jQuery:
$(document).ready(function () {
$('select[id^=dropdown]').children().each(function () {
colors = { "N": "lightgrey", "G": "green", "O": "orange", "A": "yellow", "R": "red", "U": "purple" }
$(this).attr('style', 'background-color:' + colors[$(this).val()] + ';');
});
$('select[id^=dropdown]').change(function () {
$(this).attr('style', $(this).find('option:selected').attr('style'));
}).change();
$('select[id^=dropdown]')
.mousedown(function () { $(this).css('color', 'black') })
.blur(function () { SetStyle(this) })
.change(function () { SetStyle(this) })
SetStyle('#dropdown'); // So that we style on load
function SetStyle(obj) {
var color = $(obj).find('option:selected').css('background-color');
$(obj).css({
'color': 'rgba(0,0,0,0)',
'background-color': color
});
}
});