简体   繁体   English

选择选项后如何更改 Select 标记的文本颜色

[英]How to change text color of Select tag after an Option is selected

I'm new to CSS, and the following example is confusing me.我是 CSS 的新手,下面的例子让我很困惑。 So I would like to get a better understanding.所以我想得到一个更好的理解。

Here's what I did:这是我所做的:

HTML: HTML:

<select id="dropdown" required>
      <option disabled selected value>Choose current role</option>
      <option class="option">Student</option>
      <option class="option">Full Time Job</option>
      <option class="option">Prefer Not to Say</option>
      <option class="option">Others</option>       
</select>
       

CSS: CSS:

body: {color: white;}

Either before and after any option is clicked on the webpage, the text color on the Select bar will be white.在网页上单击任何选项之前和之后,Select 栏上的文本颜色将为白色。

I tried to change the text color with below syntax but to no avail:我尝试使用以下语法更改文本颜色,但无济于事:

#dropdown {
    padding-right: 100%;
}

#dropdown:focus:after {
    color: black;
}

It only works when I take out the #dropdown declaration:它仅在我取出 #dropdown 声明时才有效:

#dropdown:focus:after {
    color: black;
}

But I want to keep the #dropdown declaration for the creating padding.但我想保留创建填充的#dropdown 声明。 Is there other way to make this work?还有其他方法可以使这项工作吗?

And why doesn't it work with both #dropdown and #dropdown:focus:after declarations?为什么它不适用于 #dropdown 和 #dropdown:focus:after 声明?

Try this:尝试这个:

 var select = document.getElementById('mySelect'); select.onchange = function () { select.className = 'redText'; }
 .redText { background-color:#F00; }
 <select id="mySelect"> <option>1</option> <option>2</option> <option>3</option> </select>

This is with javascript, i did it as easy as possible.这是 javascript,我尽可能简单地做到了。

<select> tags are difficult to style, you'll need to strip it down first by using appearance: none on the select. <select>标签很难设置样式,您需要先使用appearance: none select 上的 none。

 body { font: 2ch/1 Consolas; } fieldset { position: relative; display: inline-block; border: 0; } select { appearance: none; display: inline-block; color: tomato; height: 28px; padding: 3px 30px 3px 6px; font: inherit; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; background: #000; } select:focus { background: #000; color: lime; }.icon { position: absolute; top: 6px; right: 16px; display: inline-block; padding: 0; width: 22px; height: 27px; background: url(https://i.ibb.co/Kx33pSY/01.jpg) right / 90% no-repeat #000; pointer-events: none; }
 <fieldset> <output class='icon'></output> <select id="dropdown" required> <option disabled selected>Choose current role</option> <option class="option">Student</option> <option class="option">Full Time Job</option> <option class="option">Prefer Not to Say</option> <option class="option">Others</option> </select> </fieldset>

You can try this too:你也可以试试这个:

select option {
   background - color: white;
   font - weight: bold;
   color: red;
 }

An HTML "select" element can be styled, however very minimally.可以设置 HTML“选择”元素的样式,但非常少。 Otherwise, if you want every different option colour, give a separate class or id to all options.否则,如果您想要每个不同的选项颜色,请为所有选项提供单独的 class 或 id。

A different sort of solution would be to create your own dropdown menu using HTML, CSS and JS.另一种解决方案是使用 HTML、CSS 和 JS 创建自己的下拉菜单。 This would give you complete visual control.这将为您提供完整的视觉控制。 Styling select tags can be really tricky as every browser as their own unique way of rendering them.样式化 select 标签可能非常棘手,因为每个浏览器都有自己独特的呈现方式。

try with different browsers as well尝试使用不同的浏览器

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

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