jQuery插件如何编写03_ 简单的全选反选插件

解释一下:

     这个代码没有吧全选前面的复选框和下面的复选框进行关联,在下一篇文章( jQuery插件如何编写04_ 简单的全选反选插件(关联了主复选框和子复选框,代码简单))中,会把这个功能实现了

效果图:


代码:

 
 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全选反选</title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        ul {
            list-style: none;
        }
    </style>
</head>
<body>

<input id="checkedAllId" type="checkbox"/>全选
<input id="btnUnCheckId" type="button" value="反选">
<ul id="list">
    <li><label><input type="checkbox"/>1.时间都去哪了</label></li>
    <li><label><input type="checkbox"/>2.小苹果</label></li>
    <li><label><input type="checkbox"/>3.浪迹天涯</label></li>
    <li><label><input type="checkbox"/>4.小花花</label></li>
    <li><label><input type="checkbox"/>5.你是我的眼</label></li>
    <li><label><input type="checkbox"/>6.天南地北</label></li>
</ul>
</body>
</html>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">

//定义一个全选反选的jQuery插件
jQuery.fn.extend({
    "checkAll":function (isChecked) {
        //this是jQuery对象
        this.each(function () {
            //这个this是jQuery包装集中的每个DOM对象
            this.checked = isChecked;
        })
    },

    "uncheck":function () {
        //this是jQuery对象
        this.each(function () {
            this.checked = !this.checked;
        })
    }
});

//主函数
$(function(){
    //点击全选的复选框,调用插件中的checkAll函数
    $("#checkedAllId").click(function () {
        $("#list :checkbox").checkAll(this.checked);
    });

    //点击反选的按钮,调用插件中的uncheck函数
    $("#btnUnCheckId").click(function () {
        $("#list :checkbox").uncheck();
    });
});
</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值