简体   繁体   English

select - 如何更改所选选项背景的颜色?

[英]select - How to change the color of the selected option background?

Note: the question is not about how to change the bg color of the selected option.注意:问题不是关于如何更改所选选项的背景颜色。

When I open a select dropdown, the current selected option has a different color, when I hover with the mouse this color jump to other option that I hover on当我打开一个 select 下拉菜单时,当前选择的选项有不同的颜色,当我用鼠标 hover 这个颜色跳到我 hover 上的其他选项时

在此处输入图像描述

Is there a way to change the bg color of the current hover option?有没有办法改变当前 hover 选项的 bg 颜色?

Note: adding background-color: white;注意:添加background-color: white; in hover state doesn't seems have an effect.hover state 似乎没有效果。

See the current css见当前css

在此处输入图像描述

and in hover state并在 hover state 在此处输入图像描述

在此处输入图像描述

If you mean the color BLUE HIGHLIGHT , well, it cant be change by CSS.如果您的意思是颜色BLUE HIGHLIGHT ,那么它不能被 CSS 更改。 Because it is from the browser.因为它来自浏览器。 you can either use libraries or ul>li您可以使用librariesul>li

Please see below, what you atleast can do.请看下面,你至少可以做什么。 I hope this will help you.我希望这能帮到您。

 select option:checked { background: yellow; color: green; }
 <select> <option> I wanted to be happy </option> <option> I wanted money </option> <option> I wanted money </option> <option> I wanted money </option> </select>

Here is an alternative idea with js.这是 js 的另一种想法。 May be it could be helpful.可能会有所帮助。

 function classToSelectedOption() { var alreadySelected = document.querySelectorAll(".selected"); //Remove class to previus selected option [].forEach.call(alreadySelected, function(el) { el.classList.remove("selected"); }); //Add class to current selected option var value = document.getElementById("select").value; document.querySelector('option[ value=' + value + ']').classList.add("selected"); }
 select option { background: black; color: #fff; } select option.selected { background: red;important; }
 <select id="select" onchange="classToSelectedOption()"> <option value="first">first</option> <option value="second">second</option> <option value="third">third</option> <option value="fourth">fourth</option> </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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