我正在尝试在选中 input[type='radio'] 时更改标签的背景颜色。这是我的 HTML:
<div class="type">
<ul id="id_type">
<li><label for="id_type_0"><input type="radio" name="type" value="inc" required="" id="id_type_0" checked="">
Income</label>
</li>
<li><label for="id_type_1"><input type="radio" name="type" value="exp" required="" id="id_type_1">
Expense</label>
</li>
</ul>
</div>
...如您所见,标签包裹了 radio 输入。
这是我正在使用的选择器:
.type input[type=radio]:checked ~ label {
background-color:#e0e0e0;
}
但是没有成功。
我该如何解决这个问题?
我无法更改 HTML,它是由 Django 生成的。
最佳答案
.type input[type=radio]:checked+label {
background-color: #e0e0e0;;
}
<div class="type">
<ul id="id_type">
<li>
<input type="radio" name="type" value="inc" required="" id="id_type_0" checked="">
<label for="id_type_0">Income</label>
</li>
<li>
<input type="radio" name="type" value="exp" required="" id="id_type_1">
<label for="id_type_1">Expense</label>
</li>
</ul>
</div>
关于javascript - 选中输入[类型 ='radio'] 时更改标签的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58881941/