<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
</style>
</head>
<body>
<input type="checkbox">你
<input type="checkbox">你
<input type="checkbox">你
<input type="checkbox">你
<input type="checkbox">你
<input type="checkbox">你
<button class="btn1">全选</button>
<button class="btn2">全不选</button>
<button class="btn3">反选</button>
</body>
<script src="./js/jquery-3.4.1.min.js"></script>
<script>
$('.btn1').click(function(){
$(':checkbox').prop('checked',true)
})
$('.btn2').click(function(){
$(':checkbox').prop('checked',false)
})
$('.btn3').click(function(){
$(':checkbox').each(function(index,item){
// console.log(index,'-------',$(item).prop('checked'));
if($(item).prop('checked')){
$(item).prop('checked',false)
}else{
$(item).prop('checked',true)
}
})
})
</script>
</html>
需要用到each方法,遍历每一个,一一对应