JavaScript實現動態生成表格案例詳解
阿新 • • 發佈:2021-12-23
目錄
- 前言
- 實現思路
- 實現程式碼
- 實現效果
前言
在這裡實現一個動態新增表格的案例,當點選新增按鈕時,可以彈出一個表單,然後將輸入的內容新增到表格中,也可以SZbJzGzPfi將表格中的整行內容清除。
實現思路
先建立一個表格和一個表單,將表單中輸入的內容動態新增進表格中,表單頁面右上角有一個關閉按鈕,當點選時,可以將表單頁面關閉並將表格頁面顯示。為了頁面美觀,我將新增資料的按鈕放在了表格的<tfoot></tfoot>中,將動態生成的表格資料新增到<tbody><tbody>中,當點選新增按鈕時,隱藏表格,並顯示錶單,在表單中填寫要新增的資訊,然後獲取輸入的資訊,通過生成表格的一行元素,並將獲得的值新增進去,最後將這一行新增到<tbody><tbody>的最後一行,當點選表單頁面的新增按鈕時,讓表單隱藏,並顯示修改後的變革,因為還要實現動態刪除功能,所以需要給表格中的每一行元素新增一個刪除屬性(超連結),當我們點選刪除時,獲取到其對應的行,進行刪除操作。
實現程式碼
<!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 { SZbJzGzPfibackground-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="scrip:;" 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."></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動態生成表格內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以客棧後多多支援我們!