jQuery实现的简单动态添加、删除表格功能示例

发布时间 - 2026-01-11 03:21:14    点击率:

本文实例讲述了jQuery实现的简单动态添加、删除表格功能。分享给大家供大家参考,具体如下:

先来看看运行效果:

具体代码如下:

<!DOCTYPE html>
<html>
<head lang="en">
  <meta charset="UTF-8">
  <title>www. </title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    .wrap {
      width: 410px;
      margin: 100px auto 0;
    }
    table {
      border-collapse: collapse;
      border-spacing: 0;
      border: 1px solid #c0c0c0;
    }
    th,
    td {
      border: 1px solid #d0d0d0;
      color: #404060;
      padding: 10px;
    }
    th {
      background-color: #09c;
      font: bold 16px "微软雅黑";
      color: #fff;
    }
    td {
      font: 14px "微软雅黑";
    }
    td a.get {
      text-decoration: none;
    }
    a.del:hover {
      text-decoration: underline;
    }
    tbody tr {
      background-color: #f0f0f0;
    }
    tbody tr:hover {
      cursor: pointer;
      background-color: #fafafa;
    }
    .btnAdd {
      width: 110px;
      height: 30px;
      font-size: 20px;
      font-weight: bold;
    }
    .form-item {
      height: 100%;
      position: relative;
      padding-left: 100px;
      padding-right: 20px;
      margin-bottom: 34px;
      line-height: 36px;
    }
    .form-item > .lb {
      position: absolute;
      left: 0;
      top: 0;
      display: block;
      width: 100px;
      text-align: right;
    }
    .form-item > .txt {
      width: 300px;
      height: 32px;
    }
    .mask {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
      background: #000;
      opacity: 0.15;
      display: none;
    }
    .form-add {
      position: fixed;
      top: 30%;
      left: 50%;
      margin-left: -197px;
      padding-bottom: 20px;
      background: #fff;
      display: none;
    }
    .form-add-title {
      background-color: #f7f7f7;
      border-width: 1px 1px 0 1px;
      border-bottom: 0;
      margin-bottom: 15px;
      position: relative;
    }
    .form-add-title span {
      width: auto;
      height: 18px;
      font-size: 16px;
      font-family: 宋体;
      font-weight: bold;
      color: rgb(102, 102, 102);
      text-indent: 12px;
      padding: 8px 0px 10px;
      margin-right: 10px;
      display: block;
      overflow: hidden;
      text-align: left;
    }
    .form-add-title div {
      width: 16px;
      height: 20px;
      position: absolute;
      right: 10px;
      top: 6px;
      font-size: 30px;
      line-height: 16px;
      cursor: pointer;
    }
    .form-submit {
      text-align: center;
    }
    .form-submit input {
      width: 170px;
      height: 32px;
    }
  </style>
</head>
<body>
<div class="wrap">
  <div>
    <input type="button" value="添加数据" id="j_btnAddData" class="btnAdd"/>
  </div>
  <table>
    <thead>
    <tr>
      <!-- <th><input type="checkbox" id="j_cbAll" /></th> -->
      <th>课程名称</th>
      <th>所属学院</th>
      <th>已学会</th>
    </tr>
    </thead>
    <tbody id="j_tb">
    <tr>
      <!-- <td><input type="checkbox"/></td> -->
      <td>JavaScript</td>
      <td>前端与移动开发学院</td>
      <td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">GET</a></td>
    </tr>
    <tr>
      <!-- <td><input type="checkbox"/></td> -->
      <td>css</td>
      <td>前端与移动开发学院</td>
      <td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">GET</a></td>
    </tr>
    <tr>
      <!-- <td><input type="checkbox"/></td> -->
      <td>html</td>
      <td>前端与移动开发学院</td>
      <td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">GET</a></td>
    </tr>
    <tr>
      <!-- <td><input type="checkbox"/></td> -->
      <td>jQuery</td>
      <td>前端与移动开发学院</td>
      <td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">GET</a></td>
    </tr>
    </tbody>
  </table>
</div>
<div id="j_mask" class="mask"></div>
<div id="j_formAdd" class="form-add">
  <div class="form-add-title">
    <span>添加数据</span>
    <div id="j_hideFormAdd">x</div>
  </div>
  <div class="form-item">
    <label class="lb" for="j_txtLesson">课程名称:</label>
    <input class="txt" type="text" id="j_txtLesson" placeholder="请输入课程名称">
  </div>
  <div class="form-item">
    <label class="lb" for="j_txtBelSch">所属学院:</label>
    <input class="txt" type="text" id="j_txtBelSch" value="前端与移动开发学院">
  </div>
  <div class="form-submit">
    <input type="button" value="添加" id="j_btnAdd">
  </div>
</div>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
  $(document).ready(function () {
    $("#j_btnAddData").click(function () {
      $("#j_mask").show();
      $("#j_formAdd").show();
      $("#j_txtLesson").val("");
      $("#j_txtBelSch").val("前端开发学院");
    });
    $("#j_hideFormAdd").click(function () {
      $("#j_mask").hide();
      $("#j_formAdd").hide();
    });
    $("#j_btnAdd").click(function () {
      var txtLesson = $("#j_txtLesson").val();
      var txtBelSch = $("#j_txtBelSch").val();
      if (txtLesson == "" || txtBelSch == "") {
        alert("课程名或者所属学院不能为空");
        return;
      }
      var str = '<tr>'
          + '<td>' + txtLesson + '</td>'
          + '<td>' + txtBelSch + '</td>'
          + '<td><a href="javascrip:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="get">GET</a></td>'
          + '</tr>';
      $("#j_tb").append(str);
      $("#j_mask").hide();
      $("#j_formAdd").hide();
    });
    $("#j_tb").on("click",".get",function(){
      $(this).parent().parent().remove();
    });
  });
</script>
</body>
</html>

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery表格(table)操作技巧汇总》、《jQuery切换特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常用插件及用法总结》、《jQuery常见经典特效汇总》及《jquery选择器用法总结》

希望本文所述对大家jQuery程序设计有所帮助。


# jQuery  # 动态  # 添加  # 删除  # 表格  # jQuery实现用户信息表格的添加和删除功能  # jQuery实现动态添加、删除按钮及input输入框的方法  # jquery Easyui Datagrid实现批量操作(编辑  # 添加)  # jquery 删除节点 添加节点 找兄弟节点的简单实现  # jQuery动态添加与删除tr行实例代码  # 使用jquery给指定的table动态添加一行、删除一行  # jQuery实现表格行和列的动态添加与删除方法【测试可用】  # JQuery EasyUI学习教程之datagrid 添加、修改、删除操作  # jQuery Tags Input Plugin(添加/删除标签插件)详解  # jQuery实现动态添加和删除input框实例代码  # 微软  # 相关内容  # 感兴趣  # 给大家  # 请输入  # 更多关于  # 所述  # 先来  # 程序设计  # 宋体  # 为空  # 操作技巧  # 选择器  # 讲述了  # del  # hover  # text  # decoration  # underline  # cursor 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: 免费视频制作网站,更新又快又好的免费电影网站?  Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言  JavaScript实现Fly Bird小游戏  Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询  大同网页,大同瑞慈医院官网?  js实现点击每个li节点,都弹出其文本值及修改  Laravel如何为API生成Swagger或OpenAPI文档  佛山网站制作系统,佛山企业变更地址网上办理步骤?  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  微信小程序 HTTPS报错整理常见问题及解决方案  利用python获取某年中每个月的第一天和最后一天  如何在香港服务器上快速搭建免备案网站?  html文件怎么打开证书错误_https协议的html打开提示不安全【指南】  标题:Vue + Vuex 项目中正确使用 JWT 进行身份认证的实践指南  如何利用DOS批处理实现定时关机操作详解  北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?  JavaScript如何操作视频_媒体API怎么控制播放  大连 网站制作,大连天途有线官网?  Laravel如何创建自定义Artisan命令?(代码示例)  Swift开发中switch语句值绑定模式  iOS中将个别页面强制横屏其他页面竖屏  如何快速查询域名建站关键信息?  详解CentOS6.5 安装 MySQL5.1.71的方法  laravel服务容器和依赖注入怎么理解_laravel服务容器与依赖注入解析  详解阿里云nginx服务器多站点的配置  Windows家庭版如何开启组策略(gpedit.msc)?(安装方法)  Gemini怎么用新功能实时问答_Gemini实时问答使用【步骤】  如何在建站宝盒中设置产品搜索功能?  PHP 实现电台节目表的智能时间匹配与今日/明日轮播逻辑  PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)  网站制作价目表怎么做,珍爱网婚介费用多少?  青岛网站建设如何选择本地服务器?  深圳防火门网站制作公司,深圳中天明防火门怎么编码?  Laravel怎么配置不同环境的数据库_Laravel本地测试与生产环境动态切换【方法】  Laravel Eloquent关联是什么_Laravel模型一对一与一对多关系精讲  如何挑选高效建站主机与优质域名?  Win10如何卸载预装Edge扩展_Win10卸载Edge扩展教程【方法】  JavaScript 输出显示内容(document.write、alert、innerHTML、console.log)  Java类加载基本过程详细介绍  如何用花生壳三步快速搭建专属网站?  如何快速启动建站代理加盟业务?  Laravel Eloquent性能优化技巧_Laravel N+1查询问题解决  🚀拖拽式CMS建站能否实现高效与个性化并存?  如何用AI帮你把自己的生活经历写成一个有趣的故事?  如何用虚拟主机快速搭建网站?详细步骤解析  惠州网站建设制作推广,惠州市华视达文化传媒有限公司怎么样?  Laravel如何配置中间件Middleware_Laravel自定义中间件拦截请求与权限校验【步骤】  Swift中swift中的switch 语句  使用Dockerfile构建java web环境  javascript中数组(Array)对象和字符串(String)对象的常用方法总结