Jquery实现表格中的查看删除修改

    首先我们在学习js的时候就有接触到表格中的一些操作,那么Jquery是一个兼容多浏览器的轻量级的javascript库,它的核心理念是写的更少,做的更多。

    现在就用学到的Jquery来做一个表格中的查看删除修改感受一下Jquery的强大。    

           第一步:我们编写html页             

<!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>查看修改删除</title>
<link rel="stylesheet" type="text/css" href="table.css">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script src="table.js"></script>
</head>
<body>
<table id="table">
	<tr>
		<th>姓名</th>
		<th>年龄</th>
		<th>职位</th>
		<th>工资</th>
		<th>操作</th>
	</tr>	
    <tr>
		<td>张三</td>
		<td>24</td>
		<td>工程师</td>
		<td>8000</td>
		<td><a href="#" class="view">查看</a><a href="#" class="del">删除</a><a href="#">修改</a></td>
	 </tr>		 
</table>
<div class="popDiv">
	<p><strong>姓名:</strong><span></span></p>
	<p><strong>年龄:</strong><span></span></p>
	<p><strong>职位:</strong><span></span></p>
	<p><strong>工资:</strong><span></span></p>
	<a href="#" class="close">关闭</a>	
</div>
</body>
</html>
            李四、王五的跟张三类似自行添加
    注:这里引用Jquery时用的是Jquery 1.10.2版本  用的不是本地文件而是百度的CDN。

    第二步:编写样式       

#table{
	border:1px solid #abcdef;
	border-collapse:collapse;
	width:600px;
}
tr{height:30px;}
th {
	border:1px solid #abcdef;	
}
td{		
	border:1px solid #abcdef;
	text-align:center;
}
td a{
     margin-right:5px ;
	color:#f00;
}
.popDiv{
	width:500px;
	padding:10px;
	border:1px solid red;
	position:absolute;
	left:50%;
	top:100px;
	background:#fff;
	display:none;
	z-index:4;
}
.popDiv p{
	border-bottom:1px solid red;
}

         最后效果如图:

   

       最后:编写的js代码     

$(document).ready(function () {

			 $('.view').click(function(){
		  
					 //添加遮罩层
					 var maskHeight=$(document).height();
					 var maskWidth=$(document).width();
					 $('<div class="mask"></div>').appendTo($('body'));
					 $('div.mask').css({
									   'opacity':0.4,					   
									   'background':'#000',
									   'position':'absolute',
									   'left':0,
									   'top':0,
									   'height':maskHeight,
									   'width':maskWidth,
									   'z-index':2	
					});
			
					//获取表格中的数据
					var arr=[];
					$(this).parent().siblings().each(function(){
						arr.push($(this).text());
					});
					 
					 $('.popDiv').show().children().each(function(i){
					   $(this).children('span').text(arr[i]);
					 });	 
	 
					 //关闭
					 $('.close').click(function(){
						$(this).parent().hide();
						$('.mask').remove();//注意这里不能用hide,因为会不断生成mask
					  });
		   });
					
		  //删除
		  $('.del').click(function(){
		      $(this).parents('tr').remove();
		  });
			
});
  

          当点击查看时:    

         点击关闭遮罩消失,点击删除即可删除。修改这里就不放代码了,有兴趣的可以自己动手写写。相信用js写过显示和遮罩这个功能的用Jquery就体会到了它的强大。

         总结:在写移除遮罩的时候想当然的也跟点击关闭的时候用.hide但是在火狐调试器下面观察到了每当点击一次查看然后关闭,遮罩层虽然是消失了但是DOM树下每次都会多产生一个div,所以得用移除。不是功能正常了就万事无忧了,最重要的是怎样优化使得代码的效率更高。







  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 46
    评论
要使用jQuery实现网页表格动态增加与删除内容,需要先在HTML创建一个表格,并给它添加一些行和列。然后,可以使用jQuery的方法来添加和删除行或者列。 下面是一个简单的例子: HTML代码: ```html <table id="myTable"> <thead> <tr> <th>Name</th> <th>Age</th> <th>Email</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td>John</td> <td>25</td> <td>john@example.com</td> <td><button class="deleteRow">Delete</button></td> </tr> <tr> <td>Jane</td> <td>30</td> <td>jane@example.com</td> <td><button class="deleteRow">Delete</button></td> </tr> </tbody> </table> <button id="addRow">Add Row</button> <button id="addColumn">Add Column</button> ``` 在这个表格,我们有一个表头和两行数据。每行有三列数据和一个删除按钮。 现在我们可以使用jQuery来为这个表格添加一些功能: ```javascript $(document).ready(function() { // 添加行 $('#addRow').click(function() { $('#myTable tbody').append('<tr><td>New Name</td><td>New Age</td><td>New Email</td><td><button class="deleteRow">Delete</button></td></tr>'); }); // 删除行 $('#myTable').on('click', '.deleteRow', function() { $(this).closest('tr').remove(); }); // 添加列 $('#addColumn').click(function() { $('#myTable thead tr').append('<th>New Column</th>'); $('#myTable tbody tr').each(function() { $(this).append('<td>New Data</td>'); }); }); }); ``` 在这个代码,我们使用了jQuery的`append()`方法来添加新的行或列。我们还使用了`closest()`方法来找到要删除的行,并使用`remove()`方法来将其从表格删除。 现在,当我们点击“Add Row”按钮时,将会在表格的最后一行添加一个新的行。当我们点击“Delete”按钮时,将会删除这一行。当我们点击“Add Column”按钮时,将会在表头和每一行的最后一列添加一个新的列。 这是一个简单的例子,你可以根据你的实际需求来修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 46
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值