试试这个代码,适用于包括IE在内的各种浏览器:
HTML
This should have a WHITE background
This should have a RED background
This should have a YELLOW background
This should have a GREEN background
CSS
#select1 {width:150px; color:rgba(0, 0, 0, 0);}
#select1:focus, #select1:focus {
color:black;
}
.white {background:white;}
.red {background:red;}
.yellow {background:yellow;}
.green {background:green}
JS
function colourFunction() {
var myselect = document.getElementById("select1"),
colour = myselect.options[myselect.selectedIndex].className;
myselect.className = colour;
myselect.blur(); //This just unselects the select list without having to click
somewhere else on the page
}
HTH 🙂