这是代码。我需要的是在选中“女性”输入时更改 .register-switch 的背景颜色。 我试过:
input[value="Female"]:checked + .register-switch {
background: red;
}
但它不起作用。不使用 JavaScript 是否可以达到预期的效果?
.register-switch {
height: 32px;
margin-bottom: 20px;
padding: 4px;
background: lightblue;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.register-switch-input {
display: none;
}
.register-switch-label {
float: left;
width: 50%;
line-height: 32px;
color: #fff;
text-align: center;
cursor: pointer;
}
.register-switch-input:checked + .register-switch-label {
font-weight: normal;
color: #666;
background: #fff;
border-radius: 2px;
background-image: -webkit-linear-gradient(top, #fefefe, #eeeeee);
background-image: -moz-linear-gradient(top, #fefefe, #eeeeee);
background-image: -o-linear-gradient(top, #fefefe, #eeeeee);
background-image: linear-gradient(to bottom, #fefefe, #eeeeee);
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
}
<div class="register-switch">
<input type="radio" name="sex" value="Male" id="sex-m" class="register-switch-input" checked>
<label for="sex-m" class="register-switch-label">Male</label>
<input type="radio" name="sex" value="Female" id="sex-f" class="register-switch-input">
<label for="sex-f" class="register-switch-label">Female</label>
</div>
最佳答案
你可以尝试这样的事情。
不是将背景颜色应用到父元素,而是创建一个 :after
伪元素
具有 100% 的高度和宽度,并将背景应用到 after
稍后可以使用 +
选择器访问它。确保在父元素上设置 position:relative
(.register-switch
)
.register-switch {
height: 32px;
margin-bottom: 20px;
padding: 4px;
position:relative;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
.register-switch-input {
display: none;
}
.register-switch-label {
float: left;
width: 50%;
line-height: 32px;
color: #fff;
text-align: center;
cursor: pointer;
}
.register-switch-label:after{
position:absolute;
content:"";
width:100%;
height:100%;
background:lightblue;
left:0;
top:0;
z-index:-1;
}
.register-switch-input:checked + .register-switch-label:after{
background:tomato;
}
.register-switch-input:checked + .register-switch-label {
font-weight: normal;
color: #666;
background: #fff;
border-radius: 2px;
background-image: -webkit-linear-gradient(top, #fefefe, #eeeeee);
background-image: -moz-linear-gradient(top, #fefefe, #eeeeee);
background-image: -o-linear-gradient(top, #fefefe, #eeeeee);
background-image: linear-gradient(to bottom, #fefefe, #eeeeee);
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
}
<div class="register-switch">
<input type="radio" name="sex" value="Male" id="sex-m" class="register-switch-input" checked>
<label for="sex-m" class="register-switch-label">Male</label>
<input type="radio" name="sex" value="Female" id="sex-f" class="register-switch-input">
<label for="sex-f" class="register-switch-label">Female</label>
</div>
关于html - 选中子输入时更改父 div 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33215859/