【发布时间】:2021-09-08 06:08:49
【问题描述】:
当我们单击单选按钮时,背景颜色和标签颜色会发生变化,我已经编写了 CSS,但它不起作用。谁能推荐我。
const handleVisibility = (e) => {
e.target.closest('.case-inner-info').querySelectorAll('.inner_div [data-ref]').forEach(i => i.style.display = i.dataset.ref === e.target.dataset.ref ? 'block' : 'none');
}
const allOnOffButtons = document.querySelectorAll("div.case-inner-info input");
allOnOffButtons.forEach(button => {
button.addEventListener('click', handleVisibility);
})
.switch-label {
position: relative;
width: 58px;
float: left;
line-height: 26px;
font-size: 16px;
font-weight: 900;
text-align: center;
cursor: pointer;
color: #051326;
z-index: 2;
}
.switch-input {
display: none;
}
.switch-input:checked .switch-label {
color: #fff;
-webkit-transition: 0.15s ease-out;
-o-transition: 0.15s ease-out;
transition: 0.15s ease-out;
-webkit-transition-property: color;
-o-transition-property: color;
transition-property: color;
}
.switch-input:checked+.switch-label-off~.switch-selection {
left: 60px;
}
.switch-selection {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
position: absolute;
left: 0;
width: 70px;
height: 40px;
border-radius: 4px;
color: #fff;
z-index: 1;
-webkit-transition: left 0.15s ease-out;
-o-transition: left 0.15s ease-out;
transition: left 0.15s ease-out;
}
.case-switch .switch-selection {
background-color: red;
}
<div class="case-inner-info">
<div class="case-switch-wrap">
<div class="inner_div">
<div class="case-info" data-ref="on">
<p> Paragrapgh1</p>
</div>
<div class="case-code-wrap" style="display: none;" data-ref="off">
<p> Paragrapgh2</p>
</div>
</div>
<div class="case-switch">
<label class="switch-label switch-label-on">
<input type="radio" class="switch-input" name="switch-toggle" data-ref="on" checked>
On</label>
<label class="switch-label switch-label-off">
<input type="radio" class="switch-input" name="switch-toggle" data-ref="off">
Off</label>
<span class="switch-selection"></span>
</div>
</div>
</div>
【问题讨论】:
-
您的输入在标签内,无法到达~.switch-selection out of it。
标签: javascript html css css-transitions