更改所选选项的颜色 - jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21159032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change color of option selected - jQuery
提问by codek
I want to change the text color of the option that is selected="selected":
我想更改 selected="selected" 选项的文本颜色:
<select class="select">
<option value="--">--</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012" selected="selected">2012</option>
<option value="2011">2011</option>
</select>
I have been trying with CSS but it seems its not possible:
我一直在尝试使用 CSS,但似乎不可能:
.select select [selected="selected"]{
color: #2b2b2b;
}
Any ideas with jQuery?
对 jQuery 有什么想法吗?
回答by Mr. Alien
I have been trying with CSS but it seems its not possible:
我一直在尝试使用 CSS,但似乎不可能:
Because you are targetting the select
tag and not option
tag also, that selector means select any select
element nested inside element having .select
class
因为您的目标是select
标签而不是option
标签,该选择器意味着选择select
嵌套在元素内的任何元素.select
class
select option[selected] {
color: red;
}
You are using class
so you can make a selector like
您正在使用,class
因此您可以制作一个选择器,例如
.select option[selected] {
color: red;
}
回答by Nitin Varpe
回答by Falguni Panchal
回答by Felix
You can do:
你可以做:
$('.select option:selected').css('color','#2b2b2b');
or if you want to fire the event every time select
option has been changed, you can do:
或者,如果您想在每次select
更改选项时触发该事件,您可以执行以下操作:
$('.select').change(function () {
$(this).find('option:selected').css('color','#2b2b2b');
});
or with just plain css:
或者只使用简单的 css:
select option:checked {
color: #2b2b2b;
}
回答by Spokey
.select option:checked {
color: red;
}
Not sure about browser compatibility on this one..
不确定这个浏览器的兼容性..
Edit: https://developer.mozilla.org/en-US/docs/Web/CSS/:checked
编辑:https: //developer.mozilla.org/en-US/docs/Web/CSS/: checked