欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實現(xiàn)動態(tài)生成表格案例詳解

 更新時間:2021年12月22日 10:46:41   作者:bear*6  
本文主要介紹了通過JavaScript實現(xiàn)一個動態(tài)添加表格的案例,當(dāng)點擊添加按鈕時,可以彈出一個表單,然后將輸入的內(nèi)容添加到表格中,也可以將表格中的整行內(nèi)容清除。感興趣的可以學(xué)習(xí)一下

前言

在這里實現(xiàn)一個動態(tài)添加表格的案例,當(dāng)點擊添加按鈕時,可以彈出一個表單,然后將輸入的內(nèi)容添加到表格中,也可以將表格中的整行內(nèi)容清除。

實現(xiàn)思路

先創(chuàng)建一個表格和一個表單,將表單中輸入的內(nèi)容動態(tài)添加進表格中,表單頁面右上角有一個關(guān)閉按鈕,當(dāng)點擊時,可以將表單頁面關(guān)閉并將表格頁面顯示。為了頁面美觀,我將添加數(shù)據(jù)的按鈕放在了表格的<tfoot></tfoot>中,將動態(tài)生成的表格數(shù)據(jù)添加到<tbody><tbody>中,當(dāng)點擊添加按鈕時,隱藏表格,并顯示表單,在表單中填寫要添加的信息,然后獲取輸入的信息,通過jquery生成表格的一行元素,并將獲得的值添加進去,最后將這一行添加到<tbody><tbody>的最后一行,當(dāng)點擊表單頁面的添加按鈕時,讓表單隱藏,并顯示修改后的變革,因為還要實現(xiàn)動態(tài)刪除功能,所以需要給表格中的每一行元素添加一個刪除屬性(超鏈接),當(dāng)我們點擊刪除時,獲取到其對應(yīng)的行,進行刪除操作。

實現(xiàn)代碼?

<!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>學(xué)號</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">添加數(shù)據(jù)</td>
                </tr>
            </tfoot>
        </table>
    <!--添加數(shù)據(jù)的表單-->
    <div id="j_formAdd" class="form-add">
        <div class="title">
            <span>添加數(shù)據(jù)</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="">學(xué)號:</label>
            <input class="txt" type="text" id="order" placeholder="請輸入學(xué)號">
        </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> 

實現(xiàn)效果

到此這篇關(guān)于JavaScript實現(xiàn)動態(tài)生成表格案例詳解的文章就介紹到這了,更多相關(guān)JavaScript動態(tài)生成表格內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論