耀极客论坛

 找回密码
 立即注册
查看: 812|回复: 0

JavaScript实现动态生成表格案例详解

[复制链接]

193

主题

-17

回帖

276

积分

中级会员

Rank: 3Rank: 3

积分
276
发表于 2022-5-8 02:05:21 | 显示全部楼层 |阅读模式
  本文主要介绍了通过JavaScript实现一个动态添加表格的案例,当点击添加按钮时,可以弹出一个表单,然后将输入的内容添加到表格中,也可以将表格中的整行内容清除。感兴趣的可以学习一下

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

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

实现代码 
  1. ‹!DOCTYPE html>
  2. ‹html>
  3. ‹head lang="en">
  4.     ‹meta charset="UTF-8">
  5.     ‹title>‹/title>
  6.     ‹style>
  7.         * {
  8.             padding: 0;
  9.             margin: 0;
  10.         }
  11.         table {
  12.             width: 410px;
  13.             margin: 100px auto 0;
  14.             text-align: center;
  15.             border-collapse: collapse;
  16.             border-spacing: 0;
  17.             border: 1px solid #ccc;
  18.         }
  19.         th,
  20.         td {
  21.             width:150px;
  22.             height: 40px;
  23.             border: 1px solid #ccc;
  24.             padding: 10px;
  25.         }
  26.         a{
  27.             text-decoration: none;
  28.         }
  29.         .btnAdd {
  30.             width: 110px;
  31.             height: 30px;
  32.             font-size: 20px;
  33.         }
  34.         .item {
  35.             position: relative;
  36.             padding-left: 100px;
  37.             padding-right: 20px;
  38.             margin-bottom: 34px;
  39.         }
  40.         .lb {
  41.             position: absolute;
  42.             left: 0;
  43.             top: 0;
  44.             display: block;
  45.             width: 100px;
  46.             text-align: right;
  47.         }
  48.        .txt {
  49.             width: 300px;
  50.             height: 32px;
  51.         }
  52.         .form-add {
  53.             position: absolute;
  54.             top: 100px;
  55.             left: 578px;
  56.             border: 1px solid #ccc;
  57.             margin-left: -197px;
  58.             padding-bottom: 20px;
  59.             display: none;
  60.         }
  61.         .title {
  62.             background-color: #f7f7f7;
  63.             border-width: 1px 1px 0 1px;
  64.             border-bottom: 0;
  65.             margin-bottom: 15px;
  66.             position: relative;
  67.         }
  68.         span {
  69.             width: auto;
  70.             height: 18px;
  71.             font-size: 16px;
  72.             color: rgb(102, 102, 102);
  73.             text-indent: 12px;
  74.             padding: 8px 0px 10px;
  75.             margin-right: 10px;
  76.             display: block;
  77.             overflow: hidden;
  78.             text-align: left;
  79.         }
  80.         .title div {
  81.             width: 16px;
  82.             height: 20px;
  83.             position: absolute;
  84.             right: 10px;
  85.             top: 6px;
  86.             font-size: 30px;
  87.             line-height: 16px;
  88.             cursor: pointer;
  89.         }
  90.         .submit {
  91.             text-align: center;
  92.         }
  93.         .submit input {
  94.             width: 170px;
  95.             height: 32px;
  96.         }
  97.     ‹/style>
  98. ‹/head>
  99. ‹body>
  100.     ‹!--按钮和表单-->
  101.         ‹table>
  102.             ‹thead>
  103.             ‹tr>
  104.                 ‹th>班级‹/th>
  105.                 ‹th>姓名‹/th>
  106.                 ‹th>学号‹/th>
  107.                 ‹th>操作‹/th>
  108.             ‹/tr>
  109.             ‹/thead>
  110.             ‹tbody id="j_tb">
  111.             ‹tr>
  112.                 ‹td>1班‹/td>
  113.                 ‹td>小王‹/td>
  114.                 ‹td>001‹/td>
  115.                 ‹td>‹a href="javascrip:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="get">删除‹/a>‹/td>
  116.             ‹/tr>
  117.             ‹tr>
  118.                 ‹td>2班‹/td>
  119.                 ‹td>小熊‹/td>
  120.                 ‹td>002‹/td>
  121.                 ‹td>‹a href="javascrip:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="get">删除‹/a>‹/td>
  122.             ‹/tr>
  123.             ‹/tbody>
  124.             ‹tfoot>
  125.                 ‹tr>
  126.                     ‹td id="j_btnAddData" class="btnAdd" colspan="4">添加数据‹/td>
  127.                 ‹/tr>
  128.             ‹/tfoot>
  129.         ‹/table>
  130.     ‹!--添加数据的表单-->
  131.     ‹div id="j_formAdd" class="form-add">
  132.         ‹div class="title">
  133.             ‹span>添加数据‹/span>
  134.             ‹div id="j_hideFormAdd">׋/div>
  135.         ‹/div>
  136.         ‹div class="item">
  137.             ‹label class="lb" for="">班级:‹/label>
  138.             ‹input class="txt" type="text" id="classes" placeholder="请输入班级">
  139.         ‹/div>
  140.         ‹div class="item">
  141.             ‹label class="lb" for="">姓名:‹/label>
  142.             ‹input class="txt" type="text" id="uname" placeholder="请输入姓名">
  143.         ‹/div>
  144.         ‹div class="item">
  145.             ‹label class="lb" for="">学号:‹/label>
  146.             ‹input class="txt" type="text" id="order" placeholder="请输入学号">
  147.         ‹/div>
  148.         ‹div class="submit">
  149.             ‹input type="button" value="添加" id="j_btnAdd">
  150.         ‹/div>
  151.     ‹/div>
  152. ‹/body>
  153. ‹/html>
  154. ‹script src="jquery.js">‹/script>
  155. ‹script>
  156.     $(function () {
  157.         $('#j_btnAddData').click(function () {
  158.             $('#j_formAdd').show();
  159.             $('table').hide()
  160.         });
  161.         $('#j_hideFormAdd').click(function () {
  162.             $('#j_formAdd').hide();
  163.             $('table').show()
  164.         });
  165.         $('#j_btnAdd').click(function () {
  166.             $('table').show()
  167.             $('#j_formAdd').hide();
  168.             var classes = $('#classes').val();
  169.             var uname = $('#uname').val();
  170.             var order = $('#order').val();
  171.    
  172.             var New =$( '‹tr>' +
  173.                             '‹td>'+classes+'‹/td>'+
  174.                             '‹td>'+uname+'‹/td>' +
  175.                             '‹td>'+order+'‹/td>' +
  176.                             '‹td>‹a href="javascrip:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="get">删除‹/a>‹/td>' +
  177.                          '‹/tr>' );
  178.             $('#j_tb').append(New);
  179.         });
  180.         $('#j_tb').on('click','.get', function () {
  181.             $(this).parent().parent().remove();
  182.         });
  183.     });
  184. ‹/script>
复制代码
实现效果

  到此这篇关于JavaScript实现动态生成表格案例详解的文章就介绍到这了,更多相关JavaScript动态生成表格内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|耀极客论坛 ( 粤ICP备2022052845号-2 )|网站地图

GMT+8, 2023-3-24 14:58 , Processed in 0.072792 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表