简体   繁体   English

样式选择元素具有与所选选项相同的样式

[英]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;
}
  1. How do I style the default select so that it matches the option style rule? 如何设置默认选择的样式,使其与选项样式规则匹配?
  2. How to get this to work on Safari? 如何使它在Safari上运行? Possibly IE I haven't checked it yet no Windows system ATM. 可能是IE,但我尚未检查它,但没有Windows系统ATM。
  3. Is there a way to get these rules to display an image as well? 有没有办法让这些规则也显示图像? I've tried a few things but seems it requires Javascript which I try to avoid when I can. 我已经尝试了一些方法,但是似乎它需要Javascript,但我会尽量避免这样做。

I made a JSFiddle to demonstrate this. 我做了一个JSFiddle来演示这一点。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM