开发者

Change the color of selected option in dropdown [duplicate]

开发者 https://www.devze.com 2023-03-19 06:03 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: HTML <select>开发者_开发百科 selected option background-color CSS style
This question already has answers here: Closed 11 years ago.

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 = '';
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消