简体   繁体   English

如何更改所选选项中的字体颜色?

[英]How to change the font color in a selected option?

I have the following HTML code: 我有以下HTML代码:

<div class="register_account">
    <form>
        <select>
            <option value="null">Select a Country</option>
            <option value="AX">Åland Islands</option>
            <option value="AF">Afghanistan</option>
            <option value="AL">Albania</option>
            <option value="DZ">Algeria</option>
        </select>
    </form>
</div>

and CSS code: 和CSS代码:

.register_account
{ 
    background: #0023C4;
    width:62%;
    padding:1.5%;
}

.register_account form select
{ 
    width:100%;
    font-size:0.8em;
    color:#000;
    padding:8px;
    margin:5px 0;
    background: none;
    border: 1px solid #fff !important;
}

Now I want to change the text color of "Select a Country" and of the "selected" option in the form. 现在我想更改表单中“选择国家/地区”和“已选择”选项的文本颜色。

PS The example is here: http://jsfiddle.net/V7C6w/ . PS示例如下: http//jsfiddle.net/V7C6w/

Edit: 编辑:

A possible solution to remedy the problem might be: 解决问题的可能解决方案可能是:

.register_account
{ 
    background: #0023C4;
    width:62%;
    padding:1.5%;
}

.register_account form select
{ 
    width:100%;
    font-size:0.8em;
    color:#fff;
    padding:8px;
    margin:5px 0;
    background: #0023C4;
    border: 1px solid #fff !important;
}

Prove yourself: http://jsfiddle.net/V7C6w/9/ 证明自己: http//jsfiddle.net/V7C6w/9/

This small line will solve the problem and give what you need i guess 这条小线将解决问题,并给出你需要的东西

AFAIK you want to show selected item on the list with different color. AFAIK您想要在列表中以不同的颜色显示所选项目。 here i used green for all item and white for default selection, try this one. 在这里我使用绿色表示所有项目,白色表示默认选择,试试这个。

and it doesn't require any java-scripts, you can achieve this with simple CSS 并且它不需要任何java脚本,您可以使用简单的CSS实现这一点

.register_account form option:not(:checked) { color: green; }

Edited 编辑

//This line allows you to change the selected menu item color individually 
.register_account form option:checked { color: #0AD; }

fiddle 小提琴

.register_account option[value=null]
{
    color:yellow;
} 

Read CSS [attribute=value] Selector CSS [attribute = value]选择器

.register_account
  { background: #0023C4;
    width:62%;
    padding:1.5%;
   }

.register_account:hover form
    { width:100%;
      font-size:0.8em;
      color:#000;
      padding:8px;
      margin:5px 0;
      background: none;
      border: 1px solid #fff !important;
     }

That should work properly, use :hover, it's a lot easier. 这应该工作正常,使用:悬停,它更容易。

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

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