1. 程式人生 > 其它 >checkbox實現全選和全不選

checkbox實現全選和全不選

技術標籤:js

表格全選

功能分析:

1.三個按鈕:全選 全不選 反選
2.滑鼠移入變色,移出復原

在這裡插入圖片描述

<body>
    <from>
    <table cellpadding="12px" cellspacing="0" border="1px">
        <thead>
        <tr>
            <td>
              選框
            </td>
            <
td> 內容 </td> </tr> </thead> <tbody> <tr onmouseover="color(this)" onmouseout="cover(this)" name="tr1" > <td><input type="checkbox" name="inp"
value="pubg"></td> <td>pubg</td> </tr> <tr onmouseover="color(this)" onmouseout="cover(this)" name="tr1"> <td><input type="checkbox" name="inp" value="王者榮耀"
></td> <td>王者榮耀</td> </tr > <tr onmouseover="color(this)" onmouseout="cover(this)" name="tr1"> <td><input type="checkbox" name="inp" value="cs"></td> <td>cs</td> </tr> </tbody> </table> </from> <input type="button" name="btn1" value="全選" onclick="selectAll()"> <input type="button" name="btn2" value="全不選" onclick="notSelect()"> <input type="button" name="btn3" value="反選" onclick="reverses()"> </body> <script> function cover(target) { target.style.background="white"; } function color(target) { target.style.background="red"; } function select1() { let inpt = document.getElementsByName("inpt"); if(inpt.checked!=true){ selectAll() }else { reverses() } } function selectAll() { let inps = document.getElementsByName("inp"); for (let i = 0; i <inps.length ; i++) { inps[i].checked=true; } } function notSelect() { let inps = document.getElementsByName("inp"); for (let i = 0; i <inps.length ; i++) { inps[i].checked=false; } } function reverses() { let inps = document.getElementsByName("inp"); for (let i = 0; i <inps.length ; i++) { inps[i].checked=!inps[i].checked; } } </script>