我想基于分配的选择选项的值,使用JS / Jquery为选择字段(HTML)中的某些选项着色。这是我的代码:
$('#selectField option[value="val1"]').css({ 'color': 'green' });
该代码在Google Chrome和Internet Explorer中完美地发挥了作用,但在Mozilla Firefox中则什么也没有发生。有没有其他浏览器建议的替代方法?解决方案必须在JS中而不在CSS中。谢谢!
最佳答案
<select id="selectField">
<option value="red">Red</option>
<option value="green">Green</option>
</select>
$('#selectField option').each(function() {
var $this = $(this);
$this.css({color: $this.val()});
});