温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JavaScript如何实现动态生成表格

发布时间:2021-12-22 12:38:30 阅读:235 作者:小新 栏目:开发技术
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章给大家分享的是有关JavaScript如何实现动态生成表格的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

前言

在这里实现一个动态添加表格的案例,当点击添加按钮时,可以弹出一个表单,然后将输入的内容添加到表格中,也可以将表格中的整行内容清除。

实现思路

先创建一个表格和一个表单,将表单中输入的内容动态添加进表格中,表单页面右上角有一个关闭按钮,当点击时,可以将表单页面关闭并将表格页面显示。为了页面美观,我将添加数据的按钮放在了表格的<tfoot></tfoot>中,将动态生成的表格数据添加到<tbody><tbody>中,当点击添加按钮时,隐藏表格,并显示表单,在表单中填写要添加的信息,然后获取输入的信息,通过jquery生成表格的一行元素,并将获得的值添加进去,最后将这一行添加到<tbody><tbody>的最后一行,当点击表单页面的添加按钮时,让表单隐藏,并显示修改后的变革,因为还要实现动态删除功能,所以需要给表格中的每一行元素添加一个删除属性(超链接),当我们点击删除时,获取到其对应的行,进行删除操作。

实现代码 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            padding0;
            margin0;
        }
        table {
            width410px;
            margin100px auto 0;
            text-align: center;
            border-collapse: collapse;
            border-spacing0;
            border1px solid #ccc;
        }
        th,
        td {
            width:150px;
            height40px;
            border1px solid #ccc;
            padding10px;
        }

        a{
            text-decoration: none;
        }
        .btnAdd {
            width110px;
            height30px;
            font-size20px;
        }
        .item {
            position: relative;
            padding-left100px;
            padding-right20px;
            margin-bottom34px;
        }

        .lb {
            position: absolute;
            left0;
            top0;
            display: block;
            width100px;
            text-align: right;
        }

       .txt {
            width300px;
            height32px;
        }
        .form-add {
            position: absolute;
            top100px;
            left578px;
            border1px solid #ccc;
            margin-left: -197px;
            padding-bottom20px;
            display: none;
        }
        .title {
            background-color#f7f7f7;
            border-width1px 1px 0 1px;
            border-bottom0;
            margin-bottom15px;
            position: relative;
        }

        span {
            width: auto;
            height18px;
            font-size16px;
            colorrgb(102102102);
            text-indent12px;
            padding8px 0px 10px;
            margin-right10px;
            display: block;
            overflow: hidden;
            text-align: left;
        }

        .title div {
            width16px;
            height20px;
            position: absolute;
            right10px;
            top6px;
            font-size30px;
            line-height16px;
            cursor: pointer;
        }

        .submit {
            text-align: center;
        }

        .submit input {
            width170px;
            height32px;
        }
    </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>

实现效果

JavaScript如何实现动态生成表格

感谢各位的阅读!关于“JavaScript如何实现动态生成表格”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×