1. 程式人生 > >DOM例題

DOM例題

logs this item 按鈕 tab class classname function body

    <body>
        <table>
            <tr>
                <td>

                    <input type="checkbox" id="check_all" />
                    <label for="check_all">全選</label>
                    <input type="checkbox" id="reverse" />
                    <label for
="reverse">反選</label> </td> </tr> <tr> <td> <input type="checkbox" class="items" /> </td> </tr> <tr> <td> <input type="checkbox" class="items" /> </td> </tr> <tr> <td> <input type="checkbox" class="items" /> </td> </tr> <tr> <td> <input type="checkbox" class="items" /> </td> </tr> <tr> <td> <input type="checkbox" class="items" /> </td> </tr> <tr> <td> <input type="checkbox" class="items" /> </td> </tr> </table> </body> </html> <script> var
check = document.getElementById("check_all"); var reverse = document.getElementById("reverse"); var items = document.getElementsByClassName("items"); check.onchange = function() { if(check.checked) { for(var x in items) { items[x].checked = true; } }
else { for(var x in items) { items[x].checked = false; } } } for(var z in items) { items[z].onchange = function() { if(!this.checked) { check.checked = false; }else{ var status = 0; for(var y in items){ if(items[y].checked == false){ status = 1; alert(y); } } if(status == 0){ check.checked = true; } } } } reverse.onclick = function(){ for(var x in items){ items[x].checked = items[x].checked?false:true; } } </script>

全選框
1.點全選 全部選擇
2.取消全選 全部取消選擇
3.全選時 取消單個選項 全選框取消選擇
4.未全選時 所有單個選項選擇 全選框選擇
5.反選按鈕

DOM例題