javascript - 使用 JavaScript 检查时将复选框/单选标签设为粗体
问题描述
我想label
在单选/复选框检查中使父元素变为粗体。它适用于复选框,但收音机的标签保持粗体。
HTML:
<h4>Radios:</h4>
<div class="checkgroup">
<label for="green"><input type="radio" name="test" id="green">Green</label>
<label for="blue"><input type="radio" name="test" id="blue">Blue</label>
<label for="red"><input type="radio" name="test" id="red">Red</label>
</div>
<hr />
<h4>Checkboxes:</h4>
<div class="checkgroup">
<label for="green-check"><input type="checkbox" id="green-check">Green</label>
<label for="blue-check"><input type="checkbox" id="blue-check">Blue</label>
<label for="red-check"><input type="checkbox" id="red-check">Red</label>
</div>
JavaScript:
function makeLabelBold() {
const radios = document.querySelectorAll("input[type='radio']");
const checkboxes = document.querySelectorAll("input[type='checkbox']");
radios.forEach((radio) => {
radio.addEventListener("click", function () {
this.checked
? (this.parentElement.style.fontWeight = "700")
: (this.parentElement.style.fontWeight = "400");
});
});
checkboxes.forEach((checkbox) => {
checkbox.addEventListener("click", function () {
this.checked
? (this.parentElement.style.fontWeight = "700")
: (this.parentElement.style.fontWeight = "400");
});
});
}
makeLabelBold();
我尝试使用change
事件而不是单击,但这不起作用。有任何想法吗?这是一支可供试用的笔。
代码笔:
解决方案
你可以在没有 JavaScript 的情况下做到这一点。您可以使用:checked
CSS 选择器。像这样的东西:
input:checked + span {
font-weight: 700;
}
<h4>Radios:</h4>
<div class="checkgroup">
<label for="green"><input type="radio" name="test" id="green"><span>Green</span></label>
<label for="blue"><input type="radio" name="test" id="blue"><span>Blue</span></label>
<label for="red"><input type="radio" name="test" id="red"><span>Red</span></label>
</div>
<hr />
<h4>Checkboxes:</h4>
<div class="checkgroup">
<label for="green-check"><input type="checkbox" id="green-check"><span>Green</span></label>
<label for="blue-check"><input type="checkbox" id="blue-check"><span>Blue</span></label>
<label for="red-check"><input type="checkbox" id="red-check"><span>Red</span></label>
</div>
如果您仍然想使用 JavaScript:
单选列表不会为其每个单选框触发事件,而只会针对已真正更改的单选框触发事件(只要我们没有以编程方式更改其值,您就可以单击它/)。我做了什么:
- 替换
this
为e.target
获取radio
您单击的内容。 - 得到它的
name
属性getAttribute("name")
name
找到所有具有相同属性的收音机`- 使用此属性从所有收音机中删除样式
- 对当前选定的样式应用样式
radio
function makeLabelBold() {
const radios = document.querySelectorAll("input[type='radio']");
const checkboxes = document.querySelectorAll("input[type='checkbox']");
radios.forEach((radio) => {
radio.addEventListener("click", function (e) {
const inputsName = e.target.getAttribute("name");
const sameNameRadios = document.querySelectorAll("[name='"+inputsName+"']");
sameNameRadios.forEach(radio=>{
radio.parentElement.style.fontWeight = "400";
});
e.target.parentElement.style.fontWeight = "700";
});
});
checkboxes.forEach((checkbox) => {
checkbox.addEventListener("click", function () {
this.checked
? (this.parentElement.style.fontWeight = "700")
: (this.parentElement.style.fontWeight = "400");
});
});
}
makeLabelBold();
<h4>Radios:</h4>
<div class="checkgroup">
<label for="green"><input type="radio" name="test" id="green">Green</label>
<label for="blue"><input type="radio" name="test" id="blue">Blue</label>
<label for="red"><input type="radio" name="test" id="red">Red</label>
</div>
<hr />
<h4>Checkboxes:</h4>
<div class="checkgroup">
<label for="green-check"><input type="checkbox" id="green-check">Green</label>
<label for="blue-check"><input type="checkbox" id="blue-check">Blue</label>
<label for="red-check"><input type="checkbox" id="red-check">Red</label>
</div>
推荐阅读
- kubernetes - Migrating ROM, RWO persistent volume claims from in-tree plugin to csi in GKE
- javascript - 如何使用按钮来操作值
- ios - 如何阻止 UITableview 一次滚动多个单元格
- javascript - 我的复选框星级评分不起作用
- java - 如何显示 5 个不同的签名和 5 个图像视图,这些图像视图之前已使用查看按钮保存到图库?
- python - Python 抓取隐藏数据
- r - 在 x 轴上带有观察值的分组条形图
- javascript - 如何让一个 HTML 元素忽略另一个元素的位置?
- python - 操作列表顺序
- android - Android Studio Profiler Network 未显示接收发送行,但显示连接和详细信息