当前位置: 移动技术网 > IT编程>开发语言>JavaScript > jquery开发中创建表格(自动增加表格)代码分享

jquery开发中创建表格(自动增加表格)代码分享

2019年03月20日  | 移动技术网IT编程  | 我要评论
jquery开发中创建表格(自动增加表格)代码分享。 . 代码如下: <!doctype html> <html dir="ltr"

jquery开发中创建表格(自动增加表格)代码分享。

. 代码如下:


<!doctype html>
<html dir="ltr" lang="zh-cn">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>jquery 表格自动增加</title>
<meta name="keywords" content="jquery, 表格, table, 自动增加" />
<meta name="description" content="jquery表格自动增加" />
<meta name="viewport" content="width=device-width" />
<meta name="copyright" content="imsole.net" />
<meta name="designer" content="sole" />
<meta name="publisher" content="imsole.net" />
<meta name="author" content="sole" />
<meta name="robots" content="all" />
<meta name="distribution" content="global" />
<style type="text/css">
table { width:800px; margin:50px auto; border-collapse:collapse; border-spacing:0; }
table tr, table th, table td { border:1px solid #ddd; font-size:12px; }
table tr td:first-child { width:30px; text-align:center; }
table td input { width:100%; height:100%; padding:5px 0;  border:0 none; }
table td input:focus { box-shadow:1px 1px 3px #ddd inset; outline:none; }
</style>
<body>
 

 

<table id="count">
 <tr><th>序号</th><th>姓名</th><th>金额[usd]</th><th>时间</th><th>项目</th><th>单位</th><th>备注</th></tr>
 <tr>
  <td>1</td>
  <td><input type="text" /></td>
  <td><input type="text" /></td>
  <td><input type="text" /></td>
  <td><input type="text" /></td>
  <td><input type="text" /></td>
  <td><input type="text" /></td>
 </tr>
</table>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function(){


/* 这是一种方法,但是不精简,不过很好理解,就像面向过程编写代码一样。
 var otable = $("#count"), otr = '', oinput = '', eele = '';
 otable.on('mouver', function(){
  otr = otable.find('tr').last();
  oinput = otr.find('input');
  eele = otr.clone();
  oinput.on('click', function(){
   var parent = $(this).parents('tr');
   if(otr.index() == parent.index()){
    otable.append(eele);
   }
  });
 });
*/

//这是第二种方法,比较精简,要看对jq的理解了。
var otable = $("#count"), inum = 1, eele = '';
otable.on('click', function(e){
 var target = e.target,
  otr = $(target).closest('tr');
 if(otr.index() == otable.find('tr').last().index()){
   inum++;
  eele = otr.clone();
  eele.find('td').eq(0).text(inum);
 }
 otable.append(eele);
 });


});
</script>
 </body>
</html>

运行看效果吧

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网