jquery:选择选项和背景颜色
问候, 有一个这样的选择框:
<select id="events">
<option value="1" style="background-color:green;">Event 1</option>
<option value="2" style="background-color:yellow;">Event 2</option>
<option value="3" style="background-color:blue;">Event 3</option>
<option value="4" style="background-color:red;">Event 4</option>
</select>
在该选择框的初始浏览器渲染以及每次完成选择时,我希望选择框“事件”获取背景颜色所选的选项。
如何通过 jquery 或常规 javascript 实现这一点?
Greetings,
Having a such select box:
<select id="events">
<option value="1" style="background-color:green;">Event 1</option>
<option value="2" style="background-color:yellow;">Event 2</option>
<option value="3" style="background-color:blue;">Event 3</option>
<option value="4" style="background-color:red;">Event 4</option>
</select>
At the initial browser render of this selectbox and each time a selection is done, I want the selectbox "events" to get the background-color of the selected option.
How can this be achieved via jquery or via regular javascript ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
已选择
selected
您可以这样做:
您可以在此处进行测试。 但是,这并不适用于所有浏览器,尤其是在 IE 中,
You can do this:
You can test it out here. But, this won't work in all browsers, especially in IE the
<select>
element is notoriously unstyleable (for lack of a better...or real word). It'll work in the browsers that support it and just have no effect in the ones that don't. You may instead wait to style a parent element to give an indicator in all browsers, something like this.IE 友好的方式,这必须应用于
option
标记。IE friendly way, this has to applied to
option
tag.