【问题标题】:Radio button does not change background color when on click HTML/CSS单击 HTML/CSS 时,单选按钮不会更改背景颜色
【发布时间】:2020-09-26 06:21:00
【问题描述】:

当我单击另一个单选按钮时,我想将单选按钮的背景颜色更改为橙​​色,它的颜色会从白色变为橙色

代码片段:

<!DOCTYPE html>
<html>
<head>
<style>
input [type="radio"]:checked,
input [type="radio"]:not(:checked) {
    position: absolute;
    left: -9999px;
}
input [type="radio"]:checked + label,
input [type="radio"]:not(:checked) + label {
    position: relative;
    padding-left: 28px;
    cursor: pointer;
    line-height: 20px;
    display: inline-block;
    color: #666;
}
input [type="radio"]:checked + label:before,
input [type="radio"]:not(:checked) + label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 18px;
    height: 18px;
    border: 1px solid #ddd;
    border-radius: 100%;
    background: white;
}
input [type="radio"]:checked + label:after,
input [type="radio"]:not(:checked) + label:after {
    content: '';
    width: 12px;
    height: 12px;
    background: orange;
    position: absolute;
    top: 4px;
    left: 4px;
    border-radius: 100%;
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
 }
 input [type="radio"]:not(:checked) + label:after {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
 }
 input [type="radio"]:checked + label:after {
     opacity: 1;
     -webkit-transform: scale(1);
     transform: scale(1);
 }
 </style>
 </head>
 <body>
    <div class="radio-1">
        <input type="radio" class="radio-buttons" id="male" name="gender" value="male">
        <label name="gender">Male</label>
    </div>
    <div class="radio-2">
        <input type="radio" class="radio-buttons" id="female" name="gender" value="female">
        <label name="gender">Female</label>
    </div>
</body>
</html>

代码未按预期工作。

请告诉我如何改进此代码以更改单选按钮的背景颜色。

任何帮助都会很棒!谢谢!

【问题讨论】:

标签: html css


【解决方案1】:

您需要在 css 选择器中输入后删除空格,以便浏览器可以在 dom 中选择正确的元素,还需要为相应单选按钮的标签添加 for="" 属性,这样当您点击标签时它也改变了相应的单选按钮,其余的代码是完美的!

<!DOCTYPE html>
<html>
<head>
<style>
input[type="radio"]:checked,
input[type="radio"]:not(:checked) {
    position: absolute;
    left: -9999px;
}
input[type="radio"]:checked + label,
input[type="radio"]:not(:checked) + label {
    position: relative;
    padding-left: 28px;
    cursor: pointer;
    line-height: 20px;
    display: inline-block;
    color: #666;
}
input[type="radio"]:checked + label:before,
input[type="radio"]:not(:checked) + label:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    width: 18px;
    height: 18px;
    border: 1px solid #ddd;
    border-radius: 100%;
    background: white;
}
input[type="radio"]:checked + label:after,
input[type="radio"]:not(:checked) + label:after {
    content: '';
    width: 12px;
    height: 12px;
    background: orange;
    position: absolute;
    top: 4px;
    left: 4px;
    border-radius: 100%;
    -webkit-transition: all 0.2s ease;
    transition: all 0.2s ease;
 }
 input[type="radio"]:not(:checked) + label:after {
    opacity: 0;
    -webkit-transform: scale(0);
    transform: scale(0);
 }
 input[type="radio"]:checked + label:after {
     opacity: 1;
     -webkit-transform: scale(1);
     transform: scale(1);
 }
 </style>
 </head>
 <body>
    <div class="radio-1">
        <input type="radio" class="radio-buttons" id="male" name="gender" value="male">
        <label name="gender" for="male">Male</label>
    </div>
    <div class="radio-2">
        <input type="radio" class="radio-buttons" id="female" name="gender" value="female">
        <label name="gender" for="female">Female</label>
    </div>
</body>
</html>

【讨论】:

    【解决方案2】:

    您发布的代码中有两个小问题。修复这些应该使按钮按您的预期工作:

    1. 在 CSS 中,input[type="radio"] 之间有空格,而您希望将其改为 input[type="radio"](因此两者之间没有空格)。
    2. 在 HTML 中,标签上必须有一个for 属性,该属性链接到随附输入的id。所以for="male" 在一个标签上,for="female" 在另一个标签上。否则点击标签不会触发点击隐藏输入。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-09
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多