jquery表格[删除,添加,清空内容]

//表格[删除,添加,清空内容]
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    
    .wrap {
      width: 300px;
      margin: 100px auto 0;
    }
    
    table {
      border-collapse: collapse;
      border-spacing: 0;
      border: 1px solid #c0c0c0;
      width: 300px;
    }
    
    th,
    td {
      border: 1px solid #d0d0d0;
      color: #404060;
      padding: 10px;
    }
    
    th {
      background-color: #09c;
      font: bold 16px "微软雅黑";
      color: #fff;
    }
    
    td {
      font: 14px "微软雅黑";
    }
    
    tbody tr {
      background-color: #f0f0f0;
      text-align: center;
    }
    
    tbody tr:hover {
      cursor: pointer;
      background-color: #fafafa;
    }
  </style>
</head>
<body>
<div class="wrap">
	<input type="button" value="清空内容" id="btn">
  <input type="button" value="添加" id="btnAdd">
  <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>红烧肉</td>
      <td>田老师</td>
      <td><a class="get">DELETE</a></td>
    </tr>
    <tr>
      <td>
        <input type="checkbox"/>
      </td>
      <td>西红柿鸡蛋</td>
      <td>田老师</td>
      <td><a class="get">DELETE</a></td>
    </tr>
    <tr>
      <td>
        <input type="checkbox"/>
      </td>
      <td>红烧狮子头</td>
      <td>田老师</td>
      <td><a class="get">DELETE</a></td>
    </tr>
    <tr>
      <td>
        <input type="checkbox"/>
      </td>
      <td>日式肥牛</td>
      <td>田老师</td>
      <td><a class="get">DELETE</a></td>
    </tr>
    
    </tbody>
  </table>
</div>
<script type="text/javascript" src="../js/jquery-1.8.3.min.js" ></script>

<script>
  $(function () {
    
    $("#j_cbAll").click(function () {
      $("#j_tb input").prop("checked",$(this).prop("checked"));
    });
    
    $("j_tb input").click(function(){
    	if($("#j_tb input:checked").length == $("#j_tb input").length){
    		$("#j_cbAll").prop("checked",true);
    	}else{
    		$("#j_cbAll").prop("checked",false);
    	}
    });
    
    
        //1. 找到清空按钮,注册点击事件,清空tbody
    $("#btn").on("click", function () {
      $("#j_tb").empty();
    });
    
    
//    //2. 找到delete,注册点击事件[不是动态,不能给新增的元素添加这个事件]
//    $(".get").on("click", function () {
//      $(this).parent().parent().remove();
//    });
    
    $("#j_tb").on("click", ".get", function () {
      $(this).parent().parent().remove();
    });
    
    
    //3. 找到添加按钮,注册点击事件
    $("#btnAdd").on("click", function () {
      $('<tr><td><input type="checkbox"/></td><td>饺子</td><td>郝老师</td> <td><a class="get">DELETE</a></td> </tr>').appendTo("#j_tb");
    });
  });
</script>


</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_39134861/article/details/79720636
今日推荐