html实现全选与取消全选,单选
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/c57aee9a4c734166b75feb400e8db26f.png)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>全选与取消全选</title>
<style>
/* 样式可以根据需要进行调整 */
.checkbox-container {
display: flex;
float:left;
flex-direction: column;
}
.checkbox-item {
display: flex;
align-items: center;
margin-bottom: 10px;
}
input[type="checkbox"] {
width: 20px;
height: 20px;
margin-right: 5px;
}
</style>
</head>
<body>
<div class="checkbox-container">
<!-- 全选单选框 -->
<label class="checkbox-item">
<input type="checkbox" id="checkAll">
全选
</label>
<!-- 其他单选框 -->
<label class="checkbox-item">
<input type="checkbox" class="individualCheckbox" value="李华">
李华
</label>
<label class="checkbox-item">
<input type="checkbox" class="individualCheckbox" value="张三">
张三
</label>
<label class="checkbox-item">
<input type="checkbox" class="individualCheckbox" value="王伟">
王伟
</label>
<label class="checkbox-item">
<input type="checkbox" class="individualCheckbox" value="赵四">
赵四
</label>
</div>
<script>
// JavaScript代码用于实现全选与取消全选功能
document.getElementById('checkAll').addEventListener('change', function () {
var checkboxes = document.querySelectorAll('.individualCheckbox');
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = this.checked;
}
});
</script>
</body>
</html>