Html 我怎样才能得到一个 css 伪元素 :checked 在没有 Javascript 的情况下在 IE8 中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17869547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:31:31  来源:igfitidea点击:

How can I get a css pseudo element :checked to work in IE8 without Javascript?

htmlformsinputchecked

提问by nthChild

I have two radio buttons, I need to set the background color on click. My code works in all browsers except for IE8. Is this possible to get this to work for IE8 without the use of Javascript?

我有两个单选按钮,我需要在点击时设置背景颜色。我的代码适用于除 IE8 之外的所有浏览器。这是否可以在不使用 Javascript 的情况下使其适用于 IE8?

<form>
    <input type="radio" id="m" name="gender" value="male">
    <label for="m">male</label>
    <input type="radio" id="f" name="gender" value="female">
    <label for="f">female</label>
</form>

input:checked + label{
    background:red;
}

http://jsfiddle.net/chrimbus/sXjyL/3/

http://jsfiddle.net/chrimbus/sXjyL/3/

回答by Renato

While IE8 understandsadjacent sibling selectors, it doesn't understandthe checkedpseudo-element, so, unfortunately, you can't make your code IE8-friendly by using CSS only.

尽管IE8理解相邻同胞选择,它不理解checked伪元素,所以,很遗憾,你不能让你的代码IE8友好仅使用CSS。

Take a look at Selectivizror IE7.jsfor a JavaScript solution.

查看SelectivizrIE7.js以获得 JavaScript 解决方案。

回答by Janrider

You can try this:

你可以试试这个:

input[checked=checked] + label{
    background:red;
}