【随记】JQuery实现表格的双击编辑功能(自动适应表格高度和宽度)

<html>
<head>
<!-- 设定JQuery的路径 -->
<script src="../jQuery/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
<!--
 $(document).ready(function(){
	 $("#tables td").dblclick(function(){
		 /*使用宽度和高度之前要保证表格已设定了宽度和高度*/
			var height=$(this).height();
			var width=$(this).width();
			
			  if($(this).children(":input").length==0){
				  $(this).html("<input type='text' style='height:"+(height-4)+"px;width: "+(width-4)+"px;' value='"+$(this).text()+"'/>");
			  }
			  $(this).children(":input").focus();

			  $(this).children(":input").blur(function(){
				   $(this).parent().html($(this).val()); 
				  });
		});
	}); 
	//-->
</script>
<style type="text/css">

tr{
	height:40px;

}
td{

	width:150px;
}

</style>

</head>

<body οnlοad="setclick()">
	<table border="1px solid black" id="tables">
		<tr >
			<td >第一行第一列</td>
			<td >第一行第二列</td>
			<td >第一行第三列</td>
			<td >第一行第四列</td>
			<td >第一行第五列</td>
		</tr>

		<tr>
			<td>第二行第一列</td>
			<td>第二行第二列</td>
			<td>第二行第三列</td>
			<td>第二行第四列</td>
			<td>第二行第五列</td>
		</tr>

		<tr>
			<td>第三行第一列</td>
			<td>第三行第二列</td>
			<td>第三行第三列</td>
			<td>第三行第四列</td>
			<td>第三行第五列</td>
		</tr>
	</table>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值