利用jquery实现全选和反选
一、展现效果
二、实现代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<input type="checkbox" id="all" />全选<br>
<!-- <button id="reverse">反选</button><br> -->
<input type="checkbox" />吃饭
<input type="checkbox" />睡觉
<input type="checkbox" />学习
<script src="js/jquery-3.6.0.js"></script>
<script>
//全选
$("#all").click(function() {
$(":checkbox").prop("checked", this.checked)
})
//反选
$(":checkbox:not(#all)").click(function() {
var status = $(":checked:not(#all)").length == $(":checkbox:not(#all)").length;
$("#all").prop("checked", status)
})
</script>
</body>
</html>