• 原生js中实现全选和反选功能


        <!DOCTYPE html>  
        <html>  
        <head lang="en">  
            <meta charset="UTF-8">  
            <title></title>  
            <style type="text/css">  
          
            </style>  
        </head>  
        <body>  
            <table border="1">  
                <tr>  
                    <th><input type="checkbox" class="choose-all-input" onclick="clickChooseAllInput()" /></th>  
                    <th>学号</th>  
                    <th>姓名</th>  
                    <th>班级</th>  
                </tr>  
                <tr>  
                    <td><input type="checkbox" class="choose-single" /></td>  
                    <td>001</td>  
                    <td>张三</td>  
                    <td>李四</td>  
                </tr>  
                 <tr>  
                    <td><input type="checkbox" class="choose-single" /></td>  
                    <td>002</td>  
                    <td>张三</td>  
                    <td>李四</td>  
                </tr>  
                 <tr>  
                    <td><input type="checkbox" class="choose-single" /></td>  
                    <td>003</td>  
                    <td>张三</td>  
                    <td>李四</td>  
                </tr>  
                 <tr>  
                    <td><input type="checkbox" class="choose-single" /></td>  
                    <td>004</td>  
                    <td>张三</td>  
                    <td>李四</td>  
                </tr>   <tr>  
                    <td><input type="checkbox" class="choose-single" /></td>  
                    <td>005</td>  
                    <td>张三</td>  
                    <td>李四</td>  
                </tr>  
                <tr>  
                    <td><input type="checkbox" class="choose-single" /></td>  
                    <td>002</td>  
                    <td>F20</td>  
                    <td>F20</td>  
                </tr>  
            </table>  
            <button onclick="clickChooseAllBtn()">全选</button>  
            <button onclick="clickChooseReverse()">反选</button>  
        </body>  
        <script type="text/javascript">  
            var chooseAllInputEle = document.getElementsByClassName("choose-all-input")[0];  
            var chooseSingleEles = document.getElementsByClassName("choose-single");  
            function clickChooseAllInput() {  
                if (chooseAllInputEle.checked) {  
                    choose("selected");  
                } else {  
                    choose("");  
                }  
            }  
            function clickChooseAllBtn() {  
                chooseAllInputEle.checked = "checked";  
                choose("selected");  
            }  
            function choose(status) {  
                for (var i = 0; i < chooseSingleEles.length; i++) {  
                    chooseSingleEles[i].checked = status;  
                }  
            }  
          
            function clickChooseReverse() {  
                for (var i = 0; i < chooseSingleEles.length; i++) {  
                    if (chooseSingleEles[i].checked) {  
                        chooseSingleEles[i].checked = "";  
                    } else {  
                        chooseSingleEles[i].checked = "checked";  
                    }  
                }  
            }  
          
        </script>  
        </html> 

  • 相关阅读:
    自定义Android Studio方法注释模板
    shell 大型脚本工具开发实战
    Shell 脚本操作数据库实战
    awk 常用选项及数组的用法和模拟生产环境数据统计
    awk 条件及循环语句和字符串函数
    awk 表达式
    awk 概述及常用方法总结
    sed 追加文件内容
    sed 修改文件内容
    sed 删除文本中的内容
  • 原文地址:https://www.cnblogs.com/huangshikun/p/7097857.html
Copyright © 2020-2023  润新知