使用jQuery动态创建一个表格(根据用户输入的内容添加一行数据,并且能逐行删除)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery实现添加、移除表格</title>
 </head>
<style>
table{
 margin:auto auto;

background-color:#9FC;}
 td{
  width:80px;}
</style>
<script language="javascript" type="text/javascript" src="../../include/jquery.js"></script>
<script language="javascript" type="text/javascript">
   $(document).ready(function (){
   
    $("#btn1").click(function (){
        //创建tr节点
    var $tr=$("<tr></tr>");  
       //遍历获取输入的内容
     $("#info input:text").each(function (index,domEle){
      //添加td节点
       var $td=$("<td></td>");
        //将内容循环添加到创建好的TD中
    $td.append($(domEle).val());
    //将td添加到创建好的TR 中
    $td.appendTo($tr);
      });
      //创建TD--删除
    var $td=$("<td><a href='#' class='del'>delete</a></td>");
      //将内容循环添加到创建好的TD中
        $td.appendTo($tr);
        $tr.appendTo("#tab");
    
       //执行删除操作
          $(".del").click(function (){
      //alert("@@@@@@@@@@@@");
      $(this).parent().parent().remove();
    
     });
     });
  
    });

 


</script>
<body>
<form id="info" method="post">
姓名:<input type="text" id="user">
班级:<input type="text" id="greed">
年龄:<input type="text" id="age">
<input type="button" id="btn1" value="添加">
</form>
<br>
<br>
<br>
<table id="tab" border="1">
<tr id="t1">
  <td>姓名</td>
  <td>班级</td>
  <td>年龄</td>
  <td>是否删除</td>
</tr>
</table>

</body>
</html>

 

效果图如下:

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值