开发者

Change the color of selected option in dropdown [duplicate]

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜