Change the color of selected option in dropdown [duplicate]
Possible Duplicate:
HTML <select>开发者_开发百科 selected option background-color CSS style
How can I change the color of the selected option in dropdown list? I need to change the color of the option which is visible when the dropdown is closed...
You can get the value from select.options[select.selectedIndex]
, so this might fit your needs: http://jsfiddle.net/6VhK8/.
var select = document.getElementById('select');
select.onchange = function() {
for(var i = 0; i < select.options.length; i++) {
if(i == select.selectedIndex) {
select.options[i]
.style.backgroundColor = 'red';
} else {
select.options[i]
.style.backgroundColor = '';
}
}
}
精彩评论