`
black_star
  • 浏览: 33294 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

可编辑的表格

阅读更多
可编辑的表格
<!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=gb2312" />
<script type="text/javascript" src="../jquery-1.3.2.min.js"></script>
<script type="text/javascript" >
$(function(){
	/*斑马条*/
	$("tbody tr:even").css("background-color","#99c");
	/*选中td*/
	$("tbody td").click(function(){
	   var numTd= $(this);
	   if(numTd.children("input").length > 0){
		//表示里面有子元素input,不处理click事件
		return false;
	}
	   var text = numTd.html();//讲内容保存
	    /*td的内容清空*/
	  numTd.html("");
	   /*选中一个td,加入input*/
	   /*去掉边框,宽度设置为td的宽度,背景色设置为td的背景色,input的内容为td的内容,设置字体大小,原本的比较小*/
	  var inputObj= $("<input type='text' />");
	  inputObj.appendTo(numTd).css("border","1").width(numTd.width()).css("background-color",numTd.css("background-color")).val(text).css('font-size','16px');
	  /*选中文本框*/
	  inputObj.trigger("focus").trigger("select");//跨浏览器 ,原本可以用inputObj.select()选中
	  /*取消文本框点击事件*/
	  /*第二次点击的时候,其实是点击input里面。input此时在td里,因此td也会点击一次,而触发下面的事件。所以使用false来阻止点击事件蔓延*/
	 $(inputObj).click(function(){
			return false;
		}) 
	/*保存inputObj输入的内容*/
	$(inputObj).keydown(function(e){
		//获取当期的键值
		var keycode = e.which;
		if(keycode ==13){
			//13表示回车,e表示事件,把内容保留
			numTd.html($(this).val());
		}
		if(keycode == 27){
			numTd.html(text);//内容还原
		}
	})
	
	/*边框问题*/
	
	
	})
})
$(function(){

$("#old").click(function(){
  $("input").trigger("focus");
});
$("#new").click(function(){
  $("input").triggerHandler("focus");
});
$("input").focus(function(){
  $("<span>Focused!</span>").appendTo("body").fadeOut(1000);
});

})
</script>
<style type="text/css">
	table{
		border:1px solid black;
		width:400px;
		border-collapse:collapse;
	}
	table td{
		border:1px solid black;
		width:200px;
	}
	table th{
		border:1px solid black;
	}
	tbody th{
		background:#3333FF;
	}
</style>
<title>无标题文档</title>
</head>

<body>
<table>
	<thead >
		<tr><th colspan="2">可编辑的表格</th></tr>
	</thead>
	<tbody>
		<tr><th>学号</th><th>姓名</th></tr>
		<tr><td>0001</td><td>王小波</td></tr>
		<tr><td>0002</td><td>李大夫</td></tr>
		<tr><td>0003</td><td>列席阿克顿</td></tr>
		<tr><td>0004</td><td>波波</td></tr>
	</tbody>
</table>
</body>
</html>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics