<strong>功能</strong>: <br>點擊單元格選中,選中過程中使用方向鍵更換選中的單元格,選中過程中按回車鍵或直接雙擊單元格進入可編輯狀態,單元格失去焦點時保存修改的內容。 <br><br><strong>主要實現思路</strong>: <br>選中,移動選中區域等都是依靠jQuery強大的API進行實現的。而可編輯的儲存格實際上是在選取儲存格時,在儲存格上面新增個input輸入域,動態的更新資料<br><br><strong>原始碼</strong>: <br>HTML程式碼: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="68807" class="copybut" id="copybut68807" onclick="doCopy('code68807')"><u>複製程式碼</u></a></span> 程式碼如下:</div> <div class="codebody" id="code68807"> <br><table class="editableTable"> <br><br> <br><table class="editableTable"> <br><br> <br><table class="editableTable"> <br><br> <br><table class="editableTable"> <br><br> <br><table class="editableTable"> <br><br> <br><table class="editableTable"> <br> <br><tr> <br><th>Item1</th> <br><th>Item2</th> <br><th>Item3</th> <br> </tr> <br> <br><tbody> <br><tr> <br><td class="editable simpleInput">arthinking</td> <br><td class="editable simpleInput">Jason</td> <td class="editable simpleInput">itzhai</td> <br> </tr> <br><tr> <div class="codetitle"> <td class="editable simpleInput">arthinking</td> <span></span><td class="editable simpleInput">Jason</td> <a style="CURSOR: pointer" data="60521" class="copybut" id="copybut60521" onclick="doCopy('code60521')"></a><td class="editable simpleInput">itzhai</td> <u></u> </div> </tr> <tr> <td class="editable simpleInput">arthinking</td> <td class="editable simpleInput">Jason</td> <td class="editable simpleInput">itzhai</td> <div class="codebody" id="code60521"> <br> </div> </tr> </tbody> <br> </table> <br><br> <br>CSS程式碼: <br><br><br><br><br>複製程式碼<br><br><br><br>複製程式碼<br><br><br><br>複製程式碼<br><br><br><br>複製程式碼<br><br><div class="codetitle">複製程式碼<span><a style="CURSOR: pointer" data="38495" class="copybut" id="copybut38495" onclick="doCopy('code38495')"><u></u>複製程式碼</a></span> </div>複製程式碼複製程式碼複製程式碼 程式碼如下: body{ text-shadow:#FFFFFF 1px 1px 1px; } .editableTable{ padding: 10px; background-color: #DDEEF6; border:1px solid #DDEEF6; -webkit-border-radius: 6px; -moz-border-radius: 6px; } .editableTable thead{ background:#FFFFCC; } td{ background:#66CCFF; cursor:pointer; } } .selectCell{ background:#6699FF; } JS程式碼: 複製程式碼複製程式碼複製程式碼複製程式碼複製程式碼複製碼 程式碼如下:<div class="codebody" id="code38495"> <br>var EdTable = function(){ <br>// 給單元格綁定事件<br>function initBindGridEvent(){ <br>$("td.editable").unbind(); <br>// 新增單元格點擊事件<br>addGridClickEvent(); <br>// 新增單元格雙擊事件<br>addGridDbClickEvent(); <br>// 新增鍵盤事件<br>addGridKeyPressEvent(); <br>// 新增鍵盤事件<br>addGridKeyPressEvent(); <br> } <br>// 將儲存格新增點選事件<br>function addGridClickEvent(){ <br>$("td.simpleInput").bind("click",function(){ <br>$('. simpleInput').each(function(){ <br>$(this).removeClass("selectCell"); <br>}); <br>// 將選取的元素新增選取樣式<br>$(this) .addClass("selectCell"); <br>}); <br>} <br>//為儲存格新增雙擊事件<br>function addGridDbClickEvent(){ <br>$("td.simpleInput").bind ("dblclick",function(){ <br>$('.simpleInput').each(function(){ <br>$(this).removeClass("selectCell"); <br>}); <br> var val=$(this).html(); <br>var width = $(this).css("width"); <br>var height = $(this).css("height"); <br>$(this).html("<input type="text" onblur="EdTable.saveEdit(this)" style='width:" width ";height:" height "; padding:0px; margin:0px;' value='" val "'>"); <br>$(this).children("input").select(); <br>}); <br>} <br>// 為儲存格新增鍵盤事件<br>function addGridKeyPressEvent(){ <br>$(document).keyup(function(event){ <br>if(event.keyCode == 37){ <br>// 左箭頭<br>var selectCell =var selectCell = $(".selectCell").prev()[0]; <br>if(selectCell != undefined){ <br>$(".selectCell").removeClass("selectCell").prev().addClass( "selectCell"); <br>} <br>} else if(event.keyCode == 38){ <br>// 上箭頭<br>var col = $(".selectCell").prevAll().length ; <br>var topCell = $(".selectCell").parent("tr").prev().children()[col]; <br>if(topCell != undefined){ <br>$(" .selectCell").removeClass("selectCell"); <br>$(topCell).addClass("selectCell"); <br>} <br>} else if(event.keyCode == 39){ <br>/ / 右箭頭<br>var selectCell = $(".selectCell").next()[0]; <br>if(selectCell != undefined){ <br>$(".selectCell").removeClass("selectCell ").next().addClass("selectCell"); <br>} <br>} else if(event.keyCode == 40){ <br>// 下箭頭<br>var col = $(". selectCell").prevAll().length; <br>var topCell = $(".selectCell").parent("tr").next().children()[col]; <br>if(topCell != undefined){ <br>$(".selectCell").removeClass("selectCell"); <br>$(topCell).addClass("selectCell"); <br>} <br>} else if(event.keyCode == 13){ <br>// 回車鍵<br>var selectCell = $(".selectCell")[0]; <br>if(selectCell != undefined){ <br>$(selectCell).dblclick (); <br>} <br>} <br>}); <br>} <br>// 單元格失去焦點後儲存表格資訊<br>function saveEdit(gridCell){ <br>var pnt=$ (gridCell).parent(); <br>$(pnt).html($(gridCell).attr("value")); <br>$(gridCell).remove(); <br>} <br> return{ <br>initBindGridEvent : initBindGridEvent, <br>saveEdit : saveEdit <br>} </div>}(); <br><strong> </strong>EditableTable.rar<br> </table> </table> </table> </table> </table> </div>