juery操作 全选 反选 取消

<body>
    <div>
        <input type="button" value="全选" onclick="checkAll()"/>
        <input type="button" value="反选" onclick="reverseAll()"/>
        <input type="button" value="取消" onclick="cancelAll()"/>
    </div>
    <table border="1px" >
        <thead>
            <th>选择</th>
            <th>IP</th>
            <th>端口号</th>
        </thead>
        <tbody id="tb">
            <tr>
                <td>
                    <input type="checkbox">
                </td>
                <td>1.1.1.1</td>
                <td>8080</td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox">
                </td>
                <td>1.1.1.1</td>
                <td>8080</td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox">
                </td>
                <td>1.1.1.1</td>
                <td>8080</td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox">
                </td>
                <td>1.1.1.1</td>
                <td>8080</td>
            </tr>
            <tr>
                <td>
                    <input type="checkbox">
                </td>
                <td>1.1.1.1</td>
                <td>8080</td>
            </tr>
        </tbody>
    </table>
<body> <script>
        function checkAll(){
            $("#tb:checkbox").prop("checked",true);}
        function cancelAll(){
            $("#tb:checkbox").prop("checked",false);}
       
        function reverseAll(){
            $("#tb:checkbox").each(function(k){
                //this代指当前循环的每一个元素
               /*
                if(this.checked){
                    this.checked=false;
                }else{
                    this.checked=true;
                }
            */
          //prop("cheched")表示获取值
          //prop("ckecked",false)表示设置值
if($(this).prop("checked")){ $(this).prop("checked",false); }else{$(this).prop("checked",true);} }) } </script>

 

posted @ 2020-05-20 10:56  白头翁z  阅读(66)  评论(0编辑  收藏  举报