javascript 全选与反选

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #box input{
                width:20px;
                height: 20px;
            }
        </style>
    </head>
    <body>
    <!--    这里是设置三个input按钮-->
        <input type="button" name="" id="qx" value="全选" />
        <input type="button" name="" id="bx" value="不选" />
        <input type="button" name="fx" id="fx" value="反选" />
        <!--下面是复选框-->
        <div id="box">
            <input type="checkbox"  /><br />
            <input type="checkbox"  /><br />    
            <input type="checkbox"  /><br />
            <input type="checkbox"  /><br />    
            <input type="checkbox"  /><br />
            <input type="checkbox"  /><br />        
        </div>
    </body>

    <script type="text/javascript">
//        获取一个id的函数
        function id(id){
            return document.getElementById(id);
        }
//      获取复选的盒子
        var oBox=id('box');
        var oIn=oBox.getElementsByTagName('input');

        id('qx').onclick=function(){//点击全选时运行函数
            for(i=0;i<oIn.length;i++){
            oIn[i].checked=true;// 让所有的复选框都选上,这里注意checked的c不能大写
            }
        }
        id('bx').onclick=function(){//点击不选时运行函数
            for(i=0;i<oIn.length;i++){
            oIn[i].checked=false;
            }
        }
            id('fx').onclick=function(){//点击反选时运行
            for(i=0;i<oIn.length;i++){//设置一个循环
            if(oIn[i].checked==true){//设置一个判定,如果单选框是选中状态
            oIn[i].checked=false;//就让单选框处于未选中状态
            }else{
                oIn[i].checked=true;//反之就让其选中
            }
            }
        }
    </script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值