Blogger Information
Blog 35
fans 0
comment 0
visits 25421
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
购物车的商品逐条删除功能--2019年5月13日22时05分
白守的博客
Original
448 people have browsed it

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        /*表格与单元素添加边框*/
        table, th, td {
            border: 1px solid black;
        }
        /*设置表格样式, 折叠边框线并设置宽度*/
        table {
            border-collapse: collapse;
            width: 600px;
        }

        /*设置标题行背景*/
        table thead tr:first-of-type {
            background-color: lightblue;
        }

        /*选择每一行的第一列*/
        table tr td:first-of-type {
            text-align: center;
        }
    </style>
</head>
<body>
        <table>
                <caption>购物车</caption>
                <thead>
                    <tr>
                        <th style="width: 30px;"><input type="checkbox" id="all"></th>
                        <th>商品</th>
                        <th>操作</th>
                    </tr>
                </thead>
            
            
                <tbody>
                    <tr>
                        <td><input type="checkbox" name="user-select"></td>
                        <td>华为HUAWEI MateBook 14 全面屏轻薄性能笔记本电脑</td>
                        <td><button>编辑</button> <button onclick="del(this)">删除</button></td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="user-select"></td>
                        <td>联想(Lenovo)拯救者Y7000英特尔酷睿i5 15.6英寸游戏笔记本电脑</td>
                        <td><button>编辑</button> <button onclick="del(this)">删除</button></td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="user-select"></td>
                        <td>小米 (MI)Ruby 2019款 15.6英寸金属轻薄笔记本电脑</td>
                        <td><button>编辑</button> <button onclick="del(this)">删除</button></td>
                    </tr>
                    <tr>
                        <td><input type="checkbox" name="user-select"></td>
                        <td>Apple Macbook Pro 13.3[带触控栏]深空灰 </td>
                        <td><button>编辑</button> <button onclick="del(this)">删除</button></td>
                    </tr>
                </tbody>
            </table>
            <button id="del-all" disabled>全部删除</button>
    <script>
        // 先获取全部选择框
    var all = document.getElementById('all');
    // 获取商品列表选择框
    var ch = document.getElementsByName('user-select');
    // 获取全部删除按钮
    var sc = document.getElementById('del-all');
// 给全选复选框, 通过input事件, 添加全选功能
    all.addEventListener('input',getall,false);
    

    // 全选事件触发函数
    function getall(){
        // 调试一下看看选中全部选择按钮有没有返回true
        console.log(all.checked);
        if(all.checked === true){
            Object.keys(ch).forEach(function(key){
                ch[key].checked = true;
            });
        // 全部删除按钮有效
        sc.disabled = false;

        }else{
            Object.keys(ch).forEach(function(key){
                ch[key].checked = false;
            });
                            // 全部删除按钮无效
                            sc.disabled = true;
        }


        }

    // 全部删除功能
    sc.addEventListener('click',sc,false);

    function sc(){
        if(confirm('真的删除吗?')){
            // 获取tbody
            var tbody =document.getElementsByTagName('table')[0].tBodies[0];
             // 将tbody
            tbody.innerHTML = '';
            // 全部删除按钮无效
            sc.disabled = true;
            // 全选按钮还原
            all.checked = false
        }
    }

    var tbody = document.getElementsByTagName('table')[0].tBodies[0];
    function del(ele) {
        if (confirm('是否删除?')) {
            // ele.parentElement: <td>
            // ele.parentElement.parentElement: <tr>
            tbody.removeChild(ele.parentElement.parentElement);
        }
    }


    </script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments