练习案例:全选全不选反选

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title>全选全不选反选</title>
    <style type="text/css">
        td{
            border-bottom: 1px solid lightgray;
            width: 100px;
            height: 25px;
        }
        .firstTd{
            width: 30px;
        }
    </style>
    <script type="text/javascript" src="jquery-1.8.3.js"></script>
    <script type="text/javascript">
        $(function(){
            var $mail=$("input[name='mail']");
            //全选
            $("#selectAll").click(function(){
                $mail.attr("checked",true);
            });

            //全不选
            $("#selectNone").click(function(){
                $mail.attr("checked",false);
            });

            //反选
            $("#deselect").click(function(){
                $mail.each(function(){
                    this.checked=!this.checked;
                });
            });

            //全选复选框
            $("#checkAll").click(function(){
                $mail.attr("checked",this.checked);
            });

            $mail.click(function(){
                var  flg=true;
                $mail.each(function(){
                    if(!this.checked){
                        flg=false;
                    }
                });
                $("#checkAll").attr("checked",flg);
            });
        });
    </script>
</head>
<body>
    <button id="selectAll">全选</button>
    <button id="selectNone">全不选</button>
    <button id="deselect">反选</button>

    <table id="mailTable" border="0" cellpadding="0" cellspacing="0" style="width: 600px">
        <tr><td class="firstTd"><input type="checkbox" id="checkAll" /></td><td>收件人</td><td>主题</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
    </table>
</body>
</html>

 

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <title></title>
    <style type="text/css">
        td{
            border-bottom: 1px solid lightgray;
            width: 100px;
            height: 25px;
        }
        .firstTd{
            width: 30px;
        }
    </style>
    <script type="text/javascript">


        window.onload=function(){

            var table = document.getElementById("mailTable");

            var trs = table.getElementsByTagName("tr");
            for(var i=1;i<trs.length;i++){

                var tr = trs.item(i);

                //鼠标进入事件
                tr.onmouseover=function(){
                    this.style.backgroundColor="lightgray";
                }

                //
                tr.onmouseout=function(){
                    this.style.backgroundColor=null;
                }
            }


            //全选

            var selectAllBtn = document.getElementById("selectAll");

            selectAllBtn.onclick=function(){

                //选中所有复选框
                var checkboxes = table.getElementsByTagName("input");
                for(var i=0;i<checkboxes.length;i++){
                    var checkboxElement = checkboxes.item(i);
                    checkboxElement.checked=true;
                }

            }

            //全不选
            var selectNoneBtn = document.getElementById("selectNone");
            selectNoneBtn.onclick=function(){
                //取消选中所有复选框
                var checkboxes = table.getElementsByTagName("input");
                for(var i=0;i<checkboxes.length;i++){
                    var checkboxElement = checkboxes.item(i);
                    checkboxElement.checked=false;
                }
            }

            //反选
            var deselectBtn = document.getElementById("deselect");
            deselectBtn.onclick=function(){
                var checkboxes = table.getElementsByTagName("input");
                for(var i=0;i<checkboxes.length;i++){
                    var checkboxElement = checkboxes.item(i);
                    if(checkboxElement.checked){
                        checkboxElement.checked=false;
                    }else{
                        checkboxElement.checked=true;
                    }
                }
            }

        }



    </script>
</head>
<body>
    <button id="selectAll">全选</button>
    <button id="selectNone">全不选</button>
    <button id="deselect">反选</button>

    <table id="mailTable" border="0" cellpadding="0" cellspacing="0" style="width: 600px">
        <tr><td class="firstTd"></td><td>收件人</td><td>主题</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
        <tr><td class="firstTd"><input type="checkbox" name="mail" /></td><td>蛋蛋</td><td>。。。</td></tr>
    </table>
</body>
</html>

 

posted @ 2016-06-22 07:30  linyongqin  阅读(160)  评论(0编辑  收藏  举报