在IE中创建自定义单选按钮

时间:2020-08-12 05:15:20

标签: html css

以下CSS样式可以使用Firefox和chrome生成自定义单选按钮,但IE中不再显示颜色。该如何解决?

我找到了使用:checked::-ms-check的解决方案,但是background-color也消失了。

.group {
    background-color: #FF9BFF;
}

.radioBtnCtrl {
    display: inline-block;
    margin-bottom: 16px;
}

.radioBtnLabel {
    display: block;
    float: right;
    position: relative;
    margin-left: 8px;
    margin-right: 34px;
  
    width: 100px;
    color: #58860B;
}

input[type="radio"].radioBtn {
    display: inline-block;
    position: relative;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;

    width: 20px;
    height: 20px;

    cursor: pointer;
    border: 2px solid #00ff00;
    border-radius: 50%;
    outline: none;
  
    -webkit-appearance: none;
}

input[type="radio"].radioBtn:checked::after {
    display: block;
    position: absolute;
    top: 3px;
    left: 3px;
    border-radius: 50%;
    height: 10px;
    width: 10px;
    content: "";
    background-color: #FF0000;
}
<div class="group">
    <label class="radioBtnCtrl" role="radio">
        <input type="radio" aria-label="accept" class="radioBtn" name="A" value="accept" checked>
        <span class="radioBtnLabel">accept</span>
    </label>
    <label class="radioBtnCtrl" role="radio">
        <input type="radio" aria-label="reject" class="radioBtn" name="A" value="reject">
        <span class="radioBtnLabel">reject</span>
    </label>
</div>

1 个答案:

答案 0 :(得分:0)

IE可能很难正确设置样式。有几件事要注意:

1:当您使用laragon之类的网络服务器在本地PC jsut上尝试您的网站时,或者仅将其显示在浏览器中而没有任何服务器时,该网站将被视为“内部网站”,这意味着IE将其兼容性检查更改为IE5或IE7等。您可以看到,当您打开Webtools(F12)时,右上角会有一个下拉列表,显示当前模拟的IE版本号。确保将其设置为IE11或您希望在其中显示的任何版本。

2:CSS不适用于IE,因为IE不支持很多有用的CSS属性。

3:我通常通过伪造按钮并使用Jquery交换类来修复此自定义的单选按钮样式。示例:

$('#label1').click(function(){
    console.log('click');
    $('.buttonlabel').removeClass('clicked');
    $(this).closest('.buttonlabel').addClass('clicked');
    $('#radio1').prop('checked', true);
});
$('#label2').click(function(){
    console.log('click');
    $('.buttonlabel').removeClass('clicked');
    $(this).closest('.buttonlabel').addClass('clicked');
    $('#radio2').prop('checked', true);
});
body{
    width: 100vw;
    height: 100vh;
    background-color: aliceblue;
}
.customRadio {
    display: flex;
    flex-direction: column;
    background-color: aqua;
}
.radio-group {
    background-color: bisque;
    height: 100%;
    width: 100px;
}
#radio1{
    position: relative;
    left: 0;
}
.buttonlabel{
    width: 20px;
    height: 15px;
    position: relative;
    border-radius: 50px;
    background: green;
    left: 0;
    margin-top: -20px;
}
.clicked {
    background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="customRadio">
    <div class="radio-group">
        <input type="radio" name="radio" id="radio1" class="radio" checked>
        <div id="label1" class="buttonlabel"></div>
        <span>accept</span>
    </div>
    <div class="radio-group">
        <input type="radio" name="radio" id="radio2" class="radio" checked>
        <div id="label2" class="buttonlabel"></div>
        <span>reject</span>
    </div>
</div>

如您所见,我将“ label-Divs”放置在单选按钮的顶部,并使用Jquery更改了选定的单选按钮。因此,我单击div而不是单选按钮并设置div的样式。关于在IE上使用纯CSS的可能性有很多讨论,但我还没有看到一个可行的示例。因此,这就是我能为您提供的全部。也许这仍然对您有所帮助,或者至少为您提供了一些了解,为什么它不起作用。 祝你好运!