<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src =" https://code.jquery.com/jquery-3.6.0.min.js "></script>
</head>
<body>
<button>全选</button>
<button>全不选</button>
<button>反选</button>
<br>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</body>
<script>
$('button').first().click(() => $(':checkbox').prop('checked', true))
$('button').first().next().click(() => $(':checkbox').prop('checked', false))
$('button').last().click(() => {
// 获取属性的时候,没有隐式迭代了,只能获取到第一个
// console.log($(':checkbox').prop('checked'));
// $(':checkbox').prop('checked', !$(':checkbox').prop('checked') ) // 错误写法
// jquery提供了一个方法,专门用于遍历jquery获取到的伪数组的
// 伪数组.each(function(index, value){})
$(':checkbox').each((i, v) => {
// console.log(i, '----------------', v);
// console.log(i, '---', $(v).prop('checked') )
// $(v).prop('checked', !$(v).prop('checked'))
v.checked = !v.checked
})
})
</script>
</html>
使用jquery实现全选反选效果
最新推荐文章于 2022-08-29 21:32:04 发布