答案 0 :(得分:1)
样式选项在不同的操作系统上存在一些问题。您是否被允许使用bootstrap-select之类的任何插件?由于元素是li,这肯定会达到你想要的效果
答案 1 :(得分:0)
在您提供的链接中,给出的答案是使用JavaScript和一些CSS操作,这在我的最终工作正常:
<!DOCTYPE html>
<html>
<head>
<style media="screen" type="text/css">
.white {background-color:white;color:red;}
.red {background-color:red;color:black;}
.yellow {background-color:yellow;color:black;}
.green {background-color:green;color:white;}
</style>
</head>
<body>
<h2>Choose Color</h2>
<select>
<option class="white">This should have a WHITE background</option>
<option class="red">This should have a RED background</option>
<option class="yellow">This should have a YELLOW background</option>
<option class="green">This should have a GREEN background</option>
</select>
<script>
var select = document.querySelector('select');
var colorChange = function(){
var color = select.options[select.selectedIndex].className;
select.className = color;
select.blur();
};
select.addEventListener('change', colorChange);
colorChange();
</script>
</body>
</html>
此外,我在几个类中添加了color
属性,以表明您可以操作的不仅仅是背景颜色。
答案 2 :(得分:0)
@Randal在这里,您可以检查您的代码是否正常工作。在底部运行代码段
<head>
<style media="screen" type="text/css">
.white {background-color:white;}
.red {background-color:red;}
.yellow {background-color:yellow;}
.green {background-color:green;}
</style>
<body>
<h2>Choose Color</h2>
<select>
<option class="white">This should have a WHITE background</option>
<option class="red">This should have a RED background</option>
<option class="yellow">This should have a YELLOW background</option>
<option class="green">This should have a GREEN background</option>
</select>