js模块——动态生成表格

HTML

    <table>
        <thead>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
                <th>成绩</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>

CSS

  <style>
        table {
            width: 500px;
            margin: 100px auto;
            border-collapse: collapse;
            text-align: center;
        }
 
        td,
        th {
            border: 1px solid #333;
        }
 
        thead tr {
            height: 40px;
            background-color: #ccc;
        }
    </style>

JS

 <script>
        // 1、创建一个以对象为元素的数组,对象之间用逗号隔开
        var datum = [
            {
                name: '小明',
                age: '15',
                grade: '77'
            },
            {
                name: '小红',
                age: '13',
                grade: '89'
            },
            {
                name: '小兰',
                age: '13',
                grade: '78'
            }
        ];
        //获取元素
        var tbody = document.querySelector('tbody');
        // 2、创建行和单元格
        //(1)创建行
        for (var i = 0; i < datum.length; i++) {  //datum的长度=对象的个数=你要创建的行数
            var tr = document.createElement('tr');  //每有一个i就创建一行
            tbody.appendChild(tr);   //用.appendChild追加到表格主体里
            //(2)创建单元格
            for (var k in datum[i]) { //假设i=0,对于datum[0],遍历其中的属性
                /* 注:  for (var k in obj) {
                       k得到的是属性名; obj[k]得到的是属性值
               } */
                var td = document.createElement('td'); // 创建单元格
                td.innerHTML = datum[i][k];   //给单元格填充内容,填充第i行:datum[i] 的第k个:datum[i][k]
                tr.appendChild(td);  //将单元格追加到每一行后面
            }
            // 3、创建“删除”单元格
            var td = document.createElement('td');
            td.innerHTML = '<a href = "javascript:;">删除</a>';
            tr.appendChild(td);
            //4、让删除单元格可以删除它们所在的行
            td.querySelector('a').onclick = function () {
                tbody.removeChild(this.parentNode.parentNode);
            }
        }
    </script>

添加数据

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        table {
            width: 410px;
            margin: 100px auto 0;
            text-align: center;
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #ccc;
        }
        th,
        td {
            width:150px;
            height: 40px;
            border: 1px solid #ccc;
            padding: 10px;
        }

        a{
            text-decoration: none;
        }
        .btnAdd {
            width: 110px;
            height: 30px;
            font-size: 20px;
        }
        .item {
            position: relative;
            padding-left: 100px;
            padding-right: 20px;
            margin-bottom: 34px;
        }

        .lb {
            position: absolute;
            left: 0;
            top: 0;
            display: block;
            width: 100px;
            text-align: right;
        }

       .txt {
            width: 300px;
            height: 32px;
        }
        .form-add {
            position: absolute;
            top: 100px;
            left: 578px;
            border: 1px solid #ccc;
            margin-left: -197px;
            padding-bottom: 20px;
            display: none;
        }
        .title {
            background-color: #f7f7f7;
            border-width: 1px 1px 0 1px;
            border-bottom: 0;
            margin-bottom: 15px;
            position: relative;
        }

        span {
            width: auto;
            height: 18px;
            font-size: 16px;
            color: rgb(102, 102, 102);
            text-indent: 12px;
            padding: 8px 0px 10px;
            margin-right: 10px;
            display: block;
            overflow: hidden;
            text-align: left;
        }

        .title div {
            width: 16px;
            height: 20px;
            position: absolute;
            right: 10px;
            top: 6px;
            font-size: 30px;
            line-height: 16px;
            cursor: pointer;
        }

        .submit {
            text-align: center;
        }

        .submit input {
            width: 170px;
            height: 32px;
        }
    </style>

</head>
<body>
    <!--按钮和表单-->
        <table>
            <thead>
            <tr>
                <th>班级</th>
                <th>姓名</th>
                <th>学号</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="j_tb">
            <tr>
                <td>1</td>
                <td>小王</td>
                <td>001</td>
                <td><a href="javascrip:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="get">删除</a></td>
            </tr>
            <tr>
                <td>2</td>
                <td>小熊</td>
                <td>002</td>
                <td><a href="javascrip:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="get">删除</a></td>
            </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td id="j_btnAddData" class="btnAdd" colspan="4">添加数据</td>
                </tr>
            </tfoot>
        </table>
    <!--添加数据的表单-->
    <div id="j_formAdd" class="form-add">
        <div class="title">
            <span>添加数据</span>
            <div id="j_hideFormAdd">×</div>
        </div>
        <div class="item">
            <label class="lb" for="">班级:</label>
            <input class="txt" type="text" id="classes" placeholder="请输入班级">
        </div>
        <div class="item">
            <label class="lb" for="">姓名:</label>
            <input class="txt" type="text" id="uname" placeholder="请输入姓名">
        </div>
        <div class="item">
            <label class="lb" for="">学号:</label>
            <input class="txt" type="text" id="order" placeholder="请输入学号">
        </div>
        <div class="submit">
            <input type="button" value="添加" id="j_btnAdd">
        </div>
    </div>
</body>
</html>

<script src="jquery.js"></script>
<script>
    $(function () {
        $('#j_btnAddData').click(function () {
            $('#j_formAdd').show();
            $('table').hide()
        });
        $('#j_hideFormAdd').click(function () {
            $('#j_formAdd').hide();
            $('table').show()
        });
        $('#j_btnAdd').click(function () {
            $('table').show()
            $('#j_formAdd').hide();
            var classes = $('#classes').val(); 
            var uname = $('#uname').val(); 
            var order = $('#order').val(); 
    
            var New =$( '<tr>' +
                            '<td>'+classes+'</td>'+
                            '<td>'+uname+'</td>' +
                            '<td>'+order+'</td>' +
                            '<td><a href="javascrip:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="get">删除</a></td>' +
                         '</tr>' );

            $('#j_tb').append(New);
        });
        $('#j_tb').on('click','.get', function () {
            $(this).parent().parent().remove();
        });
    });
</script> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值