[英]Style Select Element with same style as the selected option
I am trying to style a select element that is created in php. 我正在尝试设置在php中创建的select元素的样式。 The select code is very simple and I can style the text color of the drop down. 选择代码非常简单,我可以为下拉菜单的文本颜色设置样式。 But this doesn't style the select element when a option is selected. 但这在选择选项时不会为select元素设置样式。 Also this code doesn't work on Safari at all. 同样,此代码根本无法在Safari上运行。
Code: 码:
<div class="theme">
<select class="theme" name="select_theme">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>
</div>
CSS: CSS:
.theme {
width: 200px;
}
.theme select {
width: 200px;
background-color: rgba(200,200,200,0.8);
}
.theme select option[value="red"] {
color: red;
}
.theme select option[value="green"] {
color: green;
}
.theme select option[value="blue"] {
color: blue;
}
There's no way you can accomplish this without JavaScript: 没有JavaScript,您将无法实现:
$('select.theme').change(function () {
$(this).css('color', $(this).find('option:selected').css('color'));
}).change();
Here's your fiddle: http://jsfiddle.net/uCmSR/2/ 这是您的小提琴: http : //jsfiddle.net/uCmSR/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.