欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實現(xiàn)可編輯的表格實例講解(2)

 更新時間:2015年09月17日 14:44:31   作者:yjjm1990  
這篇文章主要內(nèi)容是JQuery實現(xiàn)可編輯的表格實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了jQuery實現(xiàn)可編輯表格的具體代碼,供大家參考,具體內(nèi)容如下

我們最終要達到的效果如下:

當單擊學號列的時候,可以進行編輯:

當單擊ESC的時候,操作取消,當單擊回車的時候,修改生效(沒有與后臺交互)
頁面代碼如下(asp.net):

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EditTable.aspx.cs" Inherits="EditTable" %> 
 
<!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 runat="server"> 
 <title></title> 
 <link href="css/eidtTable.css" rel="stylesheet" type="text/css" /> 
 <script src="js/jquery-1.9.1.min.js" type="text/javascript"></script> 
 <script src="js/eidtTable.js" type="text/javascript"></script> 
</head> 
<body> 
 <form id="form1" runat="server"> 
 <div> 
 <table> 
 <thead> 
 <tr> 
 <th colspan="2">可編輯的表格</th> 
 </tr> 
 </thead> 
 <tbody> 
 <tr> 
 <th>學號</th> 
 <th>姓名</th> 
 </tr> 
 <tr> 
 <td class="editTd">00001</td> 
 <td>小明</td> 
 </tr> 
 <tr> 
 <td class="editTd">00001</td> 
 <td>小明</td> 
 </tr> 
 <tr> 
 <td class="editTd">00001</td> 
 <td>小明</td> 
 </tr> 
 <tr> 
 <td class="editTd">00001</td> 
 <td>小明</td> 
 </tr> 
 </tbody> 
 </table> 
 </div> 
 </form> 
</body> 
</html> 

CSS(eidtTable.css)

table 
{ 
 border:1px solid black; 
 border-collapse:collapse; 
 width:500px; 
 } 
table th 
{ 
 border:1px solid black; 
 width:50%; 
 } 
table td 
{ 
 border:1px solid black; 
 width:50px; 
 } 
tbody th 
{ 
 background-color:#A3BAE9 
 }

JS(eidtTable.js):

/// <reference path="jquery-1.9.1.min.js" /> 
 
//$(document).ready(function () { 
// alert('test'); 
//}); 
 
//簡便的寫法 
$(function () { 
 $("tr:odd").css("background-color", "#ECE9D8"); 
 var objTd = $(".editTd"); 
 
 objTd.click(function () { 
 var text = $(this).html(); 
 var objThisTd = $(this); 
 
 //解決點擊文本框和td中間的空隙還是會出問題 這個問題 
 if (objThisTd.children("input").length > 0) { 
 return false; 
 } 
 
 var inputText = $("<input value='test' type='text'/>"); 
 
 inputText.width(objTd.width()).css("font-size", "16px").css("background-color", objTd.css("background-color")).css("border-width", "0").val(text); 
 
 objThisTd.html(""); 
 inputText.appendTo(objThisTd); 
 
 inputText.trigger("focus").trigger("select"); 
 
 inputText.click(function () { 
 return false; 
 }); 
 
 //這里采用的keydown事件,我試過用keyup事件無法屏蔽瀏覽器的回車頁面提交事件 
 inputText.keydown(function (event) { 
 //alert(event.keyCode); 
 var keycode = event.which; 
 if (keycode == 13) { 
 objThisTd.html($(this).val()); 
 //return false; 
 } 
 if (keycode == 27) { 
 objThisTd.html(text); 
 } 
 }); 
 }); 
 
 
}); 

以上就是實現(xiàn)可編輯的表格全部代碼,希望大家可以仔細研究,應用到自己的網(wǎng)站上。

相關文章

最新評論