动态添加行

动态添加行

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
      * {
        padding: 0;
        margin: 0;
        font-size: 14px;
        font-family: Century Gothic;
      }
      .wrap {
        width: 410px;
        margin: 100px auto 0;
      }
      table {
        border-collapse: collapse;
        border-spacing: 0;
        border: 1px solid #c0c0c0;
        width: 400px;        
      }
      tr,td {
        /*width: 25%;*/
        height: 25px;
      }
      .btnAdd {
        width: 110px;
        height: 29px;
        font-size: 20px;
        font-weight: bold;
        margin-bottom: 20px;
      }
      .form_add_title span {
        width: auto;
        height: 18px;
        font-size: 20px;
        font-family: Century Gothic;
        font-weight: bold;
        color: rgb(102,102,102);
        text-indent: 12px;
        margin-right: 10px;
        display: block;
        text-align: center;
        padding: 8px 0px 10px;
        overflow: hidden;
        background-color: #F5F5F5;
      }
      .form_add {
        position: fixed;
        top: 29%;
        left: 50%;
        margin-left: -197px;
        padding-bottom: 20px;
        background-color: #fff;
        display: none;
        border: 1px solid #080;
        text-align: center;
      }
      .form_item > .txt {
        width: 299px;
        height: 32px;
      }
      .form_add_title div {
        width: 16px;
        height: 20px;
        position: absolute;
        right: 10px;
        top: 6px;
        font-size: 29px;
        line-height: 16px;
        cursor: pointer;
      }
      .form_submit input {
        margin-top: 20px;
        width: 169px;
        height: 32px;
      }
  </style>
  <script src="jquery-1.8.3.js"></script>
  <script>
     // 页面加载的事件
     $(function(){
        // 显示对话框
        $("#addData").click(function(){
            // 显示对话框
            $("#formAdd").show();
            // 显示遮挡层
            $("#j_mask").show();
        });
        function closeKuang(){          
            // 隐藏对话框
            $("#formAdd").hide();
            // 隐藏遮挡层
            $("#j_mask").hide();
        }
        // 点击x关闭对话框
        $("#j_hideFormAdd").click(function(){
            closeKuang();
        });

        // 添加数据的按钮
        $("#j_btnAdd").click(function(){
            // 先获取课程的文本框对象
            var j_txtLesson = $("#j_txtLesson");
            // 获取学院文本框的对象
            var j_txtBelSch = $("#j_txtBelSch");

            // 创建行和列并直接加入到tbody中
            $("<tr><td>"+j_txtLesson.val()+"</td><td>"+j_txtBelSch.val()+"</td><td><a href='javascript:;' class='get'>GET</a></td></tr>").appendTo($("#j_tbody"));
            // 关闭对话框
            closeKuang();
        });

     });
  </script>
</head>
<body>
<div class="wrap">
  <div>
    <input type="button" value="添加数据" id="addData" class="btnAdd">
  </div>
  <table border="1" cellpadding="0" cellspacing="0">
      <thead>
        <th>课程名称</th>
        <th>所属学院</th>
        <th>已学会</th>
      </thead>
      <tbody id="j_tbody">
        <tr>
          <td>JavaScript</td>
          <td>前端与移动开发学院</td>
          <td><a href="javascript:;" class="get">GET</a></td>
        </tr>
        <tr>
          <td>jQuery</td>
          <td>前端与移动开发学院</td>
          <td><a href="javascript:;" class="get">GET</a></td>
        </tr>
        <tr>
          <td>HTML5</td>
          <td>前端与移动开发学院</td>
          <td><a href="javascript:;" class="get">GET</a></td>
        </tr>
        <tr>
          <td>CSS</td>
          <td>前端与移动开发学院</td>
          <td><a href="javascript:;" class="get">GET</a></td>
        </tr>
      </tbody>
  </table>
</div>
<!-- 遮挡的div -->
<div id="j_mask" class="mask"></div>
<div id="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>
  
</body>
</html>

猜你喜欢

转载自blog.csdn.net/Leon_Jinhai_Sun/article/details/86559468