js反选表格小案例

 思路:获取选择标签并进行遍历,如果标签被勾选,将表格状态设置成未勾选状态即可。 

这里需要注意:通过ID获取元素得到的是一个指定元素,如果多个元素用同一个ID,则获取第一个元素;而通过类名获取元素,得到的是一个存储元素的数组。 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .big {
            padding-top: 200px;
            height: 400px;
            width: 1000px;
            margin: auto;
            background:
                radial-gradient(circle at 50% 0,
                    rgba(255, 0, 0, 0.5),
                    rgba(255, 0, 0, 0) 70.71%),
                radial-gradient(circle at 6.7% 75%,
                    rgba(0, 0, 255, 0.5),
                    rgba(0, 0, 255, 0) 70.71%),
                radial-gradient(circle at 93.3% 75%,
                    rgba(0, 255, 0, 0.5),
                    rgba(0, 255, 0, 0) 70.71%) beige;

        }/*彩色背景*/

        #biao {
            height: auto;
            width: 100%;
            border-spacing: 0;
            border: 1px solid white;

        }

        thead {
            border: 1px solid white;
            height: 60px;
            width: 100%;
        }

        tbody {
            border: 1px solid white;
            height: 50px;
            width: 100%;
        }

        th {
            border: 1px solid white;
            margin: 0px;
        }

        tr {
            margin: 0px;
        }
    </style>
</head>

<body>
    <div class="big">
        <table id="biao">
            <thead>
                <th>
                    <input type="checkbox" id="all">
                </th>
                <th>
                    商品
                </th>
                <th>
                    价格
                </th>
            </thead>
            <tbody>
                <tr>
                    <th>
                        <input type="checkbox" class="small">
                    </th>
                    <th>
                        苹果
                    </th>
                    <th>
                        10yuan
                    </th>
                </tr>
                <tr>
                    <th>
                        <input type="checkbox" class="small">
                    </th>
                    <th>
                        香蕉
                    </th>
                    <th>
                        8yuan
                    </th>
                </tr>
                <tr>
                    <th>
                        <input type="checkbox" class="small">
                    </th>
                    <th>
                        火龙果
                    </th>
                    <th>
                        12yuan
                    </th>
                </tr>
                <tr>
                    <th>
                        <input type="checkbox" class="small">
                    </th>
                    <th>
                        荔枝
                    </th>
                    <th>
                        18yuan
                    </th>
                </tr>
            </tbody>
        </table>
    </div>
</body>
<script>
    var all = document.getElementById('all');
    var items = document.getElementsByClassName('small')
    all.onclick = function () {
        for (let i = 0; i < items.length; i++) {
            if(items[i].checked){
            items[i].checked = '' ;}
            else items[i].checked= this.checked
        }
    }
</script>

</html>

实现效果:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值