<!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>
<style type="text/css">
table{
border:1px solid black;
border-collapse:collapse;
}
table td,table th{
border:1px solid black;
width: 200px;
}
tbody th{
background-color:greenyellow;
}
</style>
<script type="text/javascript" src="jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function(){
$("tbody tr:even").css("background-color","red");
var numtd = $("tbody td:even");
numtd.click(function(){
if(numtd.children("input").length>0){
return false;
}
var inputobj = $("<input type='text'>");
var tdobj = $(this);
inputobj.css({
"border-width":"0",
"font-size":"16px",
"background-color":tdobj.css("background-color")
});
inputobj.width(tdobj.width());
var text = tdobj.html();
inputobj.val(text );
tdobj.html("");
inputobj.appendTo(tdobj);
/*inputobj.get(0).select();*/
inputobj.trigger("focus").trigger("select");
inputobj.click(function(){
return false;
});
inputobj.keyup(function(event){
var keycode = event.which;
if(keycode==13){
var myinput = $(this).val();
tdobj.html(myinput);
}else if(keycode==27){
tdobj.html(text);
}
})
});
});
</script>
</head>
<body>
<table>
<thead>
<tr >
<th colspan="2">可以编辑的表单</th>
</tr>
</thead>
<tbody>
<tr>
<th>学号</th>
<th>姓名</th>
</tr>
<tr>
<td>001</td>
<td>张三</td>
</tr>
<tr>
<td>002</td>
<td>李四</td>
</tr>
<tr>
<td>003</td>
<td>王五</td>
</tr>
<tr>
<td>004</td>
<td>李六</td>
</tr>
</tbody>
</table>
</body>
</html>