JavaScrip-—t复选框

原创2019-01-21 17:28:33109
摘要:<!-- <!DOCTYPE html><style>    #x{        width: 100px;        height: 100px;        background-color:red;  &n

<!-- <!DOCTYPE html>

<style>

    #x{

        width: 100px;

        height: 100px;

        background-color:red;

    }

</style>

<html>

    <head>

        <meta charset="utf-8">

    </head>

    <body>

        <div id="x">

            

        </div>

        <button onclick="a()">变高</button>

        <button onclick="b()">变宽</button>

        <button onclick="c()">变色</button>

        <button onclick="d()">重置</button>

        <button onclick="e()">隐藏</button>

        <button onclick="f()">显示</button>

    </body>

</html>

<script type="text/javascript">

    var x = document.getElementById('x');

    function a(){

        x.style.height = "200px";

    }

    function b(){

        x.style.width = "200px";

    }

    function c(){

        x.style.backgroundColor = "pink";

    }

    function d(){

        x.style.height = "100px";

        x.style.width = "100px";

        x.style.backgroundColor = "red";

    }

    function e(){

        x.style.visibility = "hidden";

    }

    function f(){

        x.style.visibility = "visible";

    }

</script> -->

    <style type="text/css">

        

        .box{width: 120px;height: 250px;border: 1px solid #000;border-radius: 5px;padding: 5px 10px;margin: 20px auto;}

        .box div{border-bottom: 1px solid #000;padding-bottom: 10px;margin-bottom: 8px;}

        .box input{margin: 8px;}

    </style>

<html>

    <head>

        <meta charset="utf-8">

    </head>

    <body>

<div>

    <div>

      <input type="checkbox" id="checkall" onclick="checkAll()"><label for="checkall">全选</label>

    </div>

    <input type="checkbox" name="item[]">选项1<br>

    <input type="checkbox" name="item[]">选项2<br>

    <input type="checkbox" name="item[]">选项3<br>

    <input type="checkbox" name="item[]">选项4<br>

    <input type="checkbox" name="item[]">选项5<br>

    <input type="checkbox" name="item[]">选项6<br>

</div>

    </body>

</html>

<script type="text/javascript">

        function checkAll(){

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

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

            for(var i = 0;i < item.length ; i++)

            {

                if(checkall.checked){

                    item[i].checked = true;

                }else{

                    item[i].checked = false;

                }

            }

        }

</script>


批改老师:天蓬老师批改时间:2019-01-21 17:54:00
老师总结:全选的重点在于复选框的名称, 因为他们是一组元素, 要注意, 还有, 提交作业 用点, 不要这样把代码一复制了事, 应该写上注释, 附上图, 并且将代码放在代码块中高亮提交, 作业可以看出一个人做事的态度, 希望下次看到你的变化

发布手记

热门词条