在未打开、聚焦的下拉框上更改IE背景颜色

20

我想在IE中,当下拉菜单聚焦时更改蓝色背景颜色,但我似乎找不到任何可以实现这一点的CSS。

<select id=focusSelect><option>Option</option></select>

JS:

document.getElementById("focusSelect").focus();

CSS:

select:focus{
    background-color: red;
}

http://jsfiddle.net/TafDD/3/

如果下拉菜单未打开,可以使用以下方法来设置其样式,但我无法确定是否有可能完全消除蓝色背景。

enter image description here

此外,设置option的背景颜色也不能清除蓝色背景。

option {
    background-color: green;
}

http://jsfiddle.net/srycroft/yE2Zg/


IE6?7?8?哪个重要? - Diodeus - James MacFarlane
我没有具体说明,因为在所有的情况下它都是蓝色的。 - ScottR
可能是HTML <select>选定选项背景颜色CSS样式的重复问题。 - EricLaw
这不是与提到的问题重复,因为解决方案并没有移除下拉菜单中的蓝色区域,而这正是本问题的目的。此外,那个问题是针对所选选项的,而这个问题是针对蓝色区域的定义 - 它们是不同的。 - ScottR
@ScottR - 你找到解决方案了吗?我也遇到了同样的问题,到目前为止,谷歌上唯一相关的结果就是这个问题 :) - JSP64
不行。我能想到的唯一方法是使用自定义下拉菜单(例如Chosen http://harvesthq.github.io/chosen/)。 - ScottR
4个回答

57

在Internet Explorer 11/Edge中(不确定之前的版本),您可以这样做:

select:focus::-ms-value {
    color: black; 
    background: red;
}

因为默认情况下字体颜色为白色(原来是为了与蓝色形成对比),所以您还应该指定字体颜色,因此您也需要覆盖它。

这里是一个dabblet演示


1
我使用以下CSS属性来使IE11中的焦点指示类似于Firefox,即一个点状轮廓线:background: transparent; color: inherit; outline-style: dotted; outline-width: thin; - Matthew Wise
1
接受这种方式是目前做法。然而,对于IE < 10的答案是“无法实现”。 - ScottR

6

感谢您阅读这篇有关旧版本IE浏览器下如何防止选中下拉选择器中蓝色背景的问题的文章。可以尝试使用WillRice提到的MS伪元素-ms-value。但是,请注意,还需要设置颜色CSS属性以使文本颜色恢复默认值(白色)。

select::-ms-value {
    background: none; /* remove blue background on ie10/ie11 when selected*/
    color:#000;
}

更多信息请点击此处


+1 - 设置前景色的注释现已纳入接受的答案,但在此撰写时尚未纳入。我也喜欢没有:focus,这样它可能还可以适用于一些尚未知的IE/Edge怪异情况(并节省一些字节)... - Jake

2
我使用以下CSS,最新版本的IE11、Edge、Firefox和Chrome都可以正常工作(我没有在早期版本中进行测试)。如果您不需要它们,请删除border-radius和padding。感谢willrice的贡献:
select {
  -webkit-appearance: none;
  -moz-appearance: none;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  padding: 5px;
}

select:focus::-ms-value {
  background: white;
  color: black;
}

select::-ms-expand {
  display: none;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  padding: 5px;
}

-3

我一直在尝试使用CSS和JavaScript,并在互联网上搜索解决方案。不幸的是,看起来无法更改IE的蓝色高亮本身。在下面的示例中,我使用了CSS和JS的组合,在IE中实现了几乎与http://jsfiddle.net/TafDD/3/相同的结果。请查看。

一个例子胜过千言万语:(在IE7中测试)

<!DOCTYPE html>
<html>
<head>
    <title>CSS Form Select Focus Color Change Test Page</title>
    <style type="text/css">
        /* Set the desired background color for the whole select element */
        form select {
            background-color: #fff;
        }

        form select option {
            background: transparent;
        }

        /* Set the desired color for the focus state */
        select:focus, select.focus {
            background-color: #f00;
            outline: none;
        }
    </style>
</head>
<body>

    <form action="" method="POST">
        <div id="selectWrap">
            <select id="focusSelect" name="test_select">
                <option value="1">Option 1</option>
                <option value="2">Option 2</option>
                <option value="3">Option 3</option>
            </select>
        </div>
    </form>

    <!--[if lt IE 9]><script>
        // NOTE: This is a pure JavaScript variant. 
        // You could also use something like jQuery.

        var selectBox = document.getElementById('focusSelect');

        // This will add the .focus class to the select 
        // giving it the defined background color
        selectBox.onfocusin = function() {
            this.className = 'focus';
        };

        // and this will restore the original background 
        // color by removing the .focus class
        selectBox.onfocusout = function() {
            this.className = '';
        };

        // This removes the blue highlight after an option is selected
        selectBox.onchange = function() {
            this.blur();
        };
    </script><![endif]-->

</body>
</html>


希望这可以帮助到您。

我还建议您看一下以下内容:

...以及40种技术的概述:

这些网站将为您提供有关如何使用CSS和/或JavaScript进一步样式化选择框的信息。

祝愉快阅读,编码愉快!


资源列表并不是一个答案。当然,如果您不使用<select>元素,您可以以任何您喜欢的方式进行样式设置,但这并不能回答问题。 - ScottR
@ScottR 我已经更新了我的回答,并附上了我提出的解决方案的示例。 - Markus Hofmann
谢谢,但我想改变聚焦选择的颜色,而不是使选择失去焦点。这仍然没有回答问题。 - ScottR
@ScottR 答案已更新。希望现在更接近您想要的结果了。 - Markus Hofmann
不行,不起作用。我用你的代码创建了一个小测试。http://jsfiddle.net/5HvuR/。我宁愿不在选择器上保留状态,但我可以理解为什么这可能是必要的。 - ScottR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接