全选功能的实现

Original 2019-06-29 14:19:36 191
abstract:全选案例代码:<!doctype html> <html> <head>     <meta charset="UTF-8">     <title>全选</title>    

全选案例代码:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>全选</title>
    <style>
        #box {
            width: 100px;

            border-radius: 10px;
            border: 1px solid red;
            text-align: center;
            padding-bottom: 10px;
        }

        #box div {
            border-bottom: 1px solid #CCCCCC;
            height: 30px;
            line-height: 30px;
            text-align: center;
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
<div id="box">
    <div>
        <input type="checkbox" id="checkall" onclick="checkall()"><label for="checkall">全选</label>
    </div>
    <input type="checkbox" class="item[]">选项一<br>
    <input type="checkbox" class="item[]">选项二<br>
    <input type="checkbox" class="item[]">选项三<br>
    <input type="checkbox" class="item[]">选项四<br>
    <input type="checkbox" class="item[]">选项五<br>
    <input type="checkbox" class="item[]">选项六<br>

</div>

<script>
    function checkall() {

        var all = document.getElementById('checkall');

        var item = document.getElementsByClassName('item[]');

        for(var i = 0;i<item.length;i++){
            if(all.checked){
                item[i].checked = true;

            }else {
                item[i].checked = false;
            }
        }
    }
</script>
</body>
</html>

运行截图

QQ截图20190629141421.png

总结:

熟悉了函数的调用过程,函数内的循环,判断流程也能掌握其原理。

每一步运行的过程基本掌握。

Correcting teacher:天蓬老师Correction time:2019-07-01 17:35:43
Teacher's summary:前端中的javascript一直是一个难点, 也是一个重点, 多动手是唯一的办法

Release Notes

Popular Entries