html如下:
篮球 <input type="checkbox" name="" id="">
足球 <input type="checkbox" name="" id="">
电竞 <input type="checkbox" name="" id="">
<button >全选</button>
<button >取消全选</button>
$.fn.extend()方法如下:
//循环遍历button
$("button").each(function (index, ele) {
console.log(index,ele);
//判断是哪一个按钮
if (index == 0) {
//单击事件
$(this).click(function () {
//全选
$("input[type='checkbox']").check();
});
} else {
//单击事件
$(this).click(function () {
//全选
$("input[type='checkbox']").discheck();
});
}
});
$.fn.extend({
check: function () {
console.log($(this))
this.each(function () {
console.log(this)
this.checked = true;
});
},
discheck: function () {
this.each(function () {
this.checked = false;
});
}
});