zoukankan      html  css  js  c++  java
  • table添加行

     

    需求是要实现表格的动态增加与删除,并且保留标题行和首行,找了半天jq插件,没找到合适的,所以自己写了个demo

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style type="text/css">
    	table.table input{ /*可输入区域样式*/ 
    100%; 
    height: 100%;
     
    
    border:none; /* 输入框不要边框 */ 
    font-family:Arial; 
    } 
    </style>
    </head>
    <body>
    <br>
    
    <table class="table" border="1">
    	<thead>
    	<tr>
    		<th>检项</th>
    		<th>厚度</th>
    		<th>光度</th>
    		<th>外观</th>
    	</tr>
    	</thead>
    	<tbody>
    	<tr id="a">
    		
    		<td class="td"></td>
    		<td><input type="" name=""></td>
    		<td><input type="" name=""></td>
    		<td><input type="" name=""></td>
    		
    	</tr>
    	<tr>
    		
    		<td class="td"></td>
    		<td><input type="" name=""></td>
    		<td><input type="" name=""></td>
    		<td><input type="" name=""></td>
    		 
    	</tr>
    		
    	</tbody>
    	
    </table>
    
    <button onclick="fun()">增加一行</button>
    <button onclick="del()">删除一行</button>
    
    <script type="text/javascript">                   //前面的序号1,2,3......
    	var i = 1;
    	$(".td").each(function(){
    		$(this).html(i++);
    	})
    
    
    	function fun(){
    		var $td = $("#a").clone();       //增加一行,克隆第一个对象
    		$(".table").append($td);
    		var i = 1;
    		$(".td").each(function(){       //增加一行后重新更新序号1,2,3......
    		$(this).html(i++);
    		})
    		$("table tr:last").find(":input").val('');   //将尾行元素克隆来的保存的值清空
    	}
    
    	function del(){
    		$("table tr:not(:first):not(:first):last").remove();      //移除最后一行,并且保留前两行
    	}
    </script>
     </body>	
    </html>
  • 相关阅读:
    html5 canvas雨点打到窗玻璃动画
    html5跟随鼠标炫酷网站引导页动画特效
    如何实现复选框的全选和取消全选效果
    CSS3透明属性opacity
    jQuery实现方式不一样的跳转到底部
    ul li设置横排,并除去li前的圆点
    jQuery美女幻灯相册轮播源代码
    微软modern.IE网站,多版本IE免费测试工具集
    css中position与z-index
    C#一个方法返回多个值
  • 原文地址:https://www.cnblogs.com/RonnieQin/p/8037970.html
Copyright © 2011-2022 走看看