html+css+js 设置全选全不选,并且复选框全部选择后,全选框自动选择的代码

效果如下:
在这里插入图片描述

html文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title>Title</title>
	<script src="js/全选和取消全选.js"></script>
	<style>
		table {
			margin: 100px auto;
			border:1px solid #333;
			
			text-align: center;
		}
		td{
			border: 1px solid #333;
			height: 70px;
			width:80px;
		}
		thead tr {
			font-weight: bold;
		}
	</style>
</head>
<body>
	<table>
		<thead>
			<tr>
				<td><input type="checkbox">全选</td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td><input type="checkbox">选择</td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td><input type="checkbox">选择</td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
			<tr>
				<td><input type="checkbox">选择</td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
			</tr>
		</tbody>
	</table>
</body>
</html>

js文件

 window.onload = function () {
	var aInput = document.getElementsByTagName('input');
	for(var i=1; i<aInput.length; i++)
	{
		aInput[i].onclick=function () {
			if(!this.checked)
			{
				aInput[0].checked=false;
			}
			var selected=0;
			for(var i=1;i<aInput.length;i++){
				if(aInput[i].checked){
					selected+=1;
				}
			}
			if(selected==aInput.length-1){
				aInput[0].checked=true;
			}
		}
		aInput[0].onclick=function () {
			for(var i=1; i<aInput.length; i++)
				aInput[i].checked=aInput[0].checked;
		}
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一个基本的HTML+CSS+jQuery示例实现,如下所示: ```html <!DOCTYPE html> <html> <head> <title>复选框操作示例</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <style type="text/css"> .container { margin: 20px; } .checkbox-group { margin-bottom: 10px; } .checkbox-group label { margin-right: 10px; } .button-group { margin-top: 10px; } </style> </head> <body> <div class="container"> <div class="checkbox-group"> <label><input type="checkbox" name="checkbox[]" value="1">选项1</label> <label><input type="checkbox" name="checkbox[]" value="2">选项2</label> <label><input type="checkbox" name="checkbox[]" value="3">选项3</label> <label><input type="checkbox" name="checkbox[]" value="4">选项4</label> <label><input type="checkbox" name="checkbox[]" value="5">选项5</label> </div> <div class="button-group"> <button id="select-all">全选</button> <button id="select-inverse">反选</button> <button id="select-none">全不选</button> </div> </div> <script type="text/javascript"> $(function() { // 全选 $('#select-all').click(function() { $('input[type="checkbox"]').prop('checked', true); }); // 反选 $('#select-inverse').click(function() { $('input[type="checkbox"]').each(function() { $(this).prop('checked', !$(this).prop('checked')); }); }); // 全不选 $('#select-none').click(function() { $('input[type="checkbox"]').prop('checked', false); }); }); </script> </body> </html> ``` 在这个示例中,我们首先定义了一个包含5个复选框的`checkbox-group`,并且定义了一个包含三个按钮的`button-group`。然后,我们使用jQuery来实现三个操作: 1. 全选:当用户点击`select-all`按钮时,我们使用`prop`方法将所有复选框的`checked`属性设置为`true`,从而实现全选的功能。 2. 反选:当用户点击`select-inverse`按钮时,我们使用`each`方法遍历所有复选框,并对每个复选框的`checked`属性取反,从而实现反选的功能。 3. 全不选:当用户点击`select-none`按钮时,我们使用`prop`方法将所有复选框的`checked`属性设置为`false`,从而实现全不选的功能。 希望这个示例能够帮助您实现您的需求!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

藏空满

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值