代码之家  ›  专栏  ›  技术社区  ›  HyperHacker

关闭时更改下拉列表的颜色

  •  2
  • HyperHacker  · 技术社区  · 16 年前

    使用CSS,我可以为下拉“选择”列表中的各个选项设置字体和背景色;但是,这些颜色仅显示在实际下拉列表中。当列表未打开时,页面上框中显示的颜色仍然是默认值。

    目前我有一个列表,有许多下拉框,有几个选项,它将是有用的,能够着色每个选项,使选择立即是显而易见的。转换为单选按钮或其他输入并不真正可行。(我的客户相当挑剔。:-p)

    2 回复  |  直到 16 年前
        1
  •  3
  •   Crescent Fresh    16 年前

    如果有什么安慰的话,我会纠正的。

    您将需要一些javascript来让它在其他浏览器中工作:

    <select onchange="this.style.backgroundColor=this.options[this.selectedIndex].style.backgroundColor">
        <option style="background-color:yellow">Item 1</option>
        <option style="background-color:lightyellow">Item 2</option>
    </select>
    

    更适合使用CSS类名:

    <select
        onchange="this.className=this.options[this.selectedIndex].className"
        class="important">
        <option class="important" selected="selected">Item 1</option>
        <option class="sorta-important">Item 2</option>
    </select>
    

    你的CSS:

    .important { background-color:yellow; }
    .sorta-important { background-color:lightyellow; }
    

    这样,您就可以将演示文稿的详细信息分开,并且使类名有意义。

        2
  •  1
  •   JMP    16 年前

    我在jquery中做到了:

    <select id="ddlColors" style="background-color: White;">
        <option style="background-color: Aqua;">1</option>
        <option style="background-color: Blue;">2</option>
        <option style="background-color: Fuchsia;">3</option>
        <option style="background-color: Gray;">4</option>
    </select>
    <script>
        $("select").change(function() {
            $("select").css("background-color", $("select option:selected").css("background-color"));
        })
        .change();
    </script>