我正在尝试使用 asp.net gridview 的键编写表格导航。这就是我到目前为止所拥有的。它工作正常,但速度很慢。请看一下,如果可能的话,让我知道如何提高性能。
基本上它是带有输入元素的 html 表。table的结构是这样的
<table>
<tr>
<td>
<div style="height:22px;">
<input type = "text">
</div>
</td>
</tr>
<tr>
<td>
<div style="height:22px;">
<input type = "text">
</div>
</td>
</tr>
...
</table>
jQuery(function($) {
$('table#<%= myTable.ClientID %>')
.bind('keydown', funcKeyDown)
});
function funcKeyDown(event)
{
//get cell element.
var cell = event.target;
//get current cellIndex
var $cell = $(cell);
var currCell = $cell.parents("td");
var cellIndex = currCell[0].cellIndex;
//get current rowIndex
var currRow = $cell.parents("tr");
var rowIndex = currRow[0].rowIndex;
var nextRowIndex, targetElem = null;
var nextCell;
switch(event.keyCode) {
case 13: //enter key
if(shiftKeyPressed == 1){
//move left
if(!(cellIndex == 0)){
targetElem = currRow.children("td").eq(cellIndex-1).find("input[type=text]");
if(targetElem){targetElem.select();}}}
else {
//move right
if(!(cellIndex == (numElements -1))){
targetElem = currRow.children("td").eq(cellIndex+1).find("input[type=text]");
if(targetElem){
targetElem.select();
}
}
}
return false;
case 16: //shift key
shiftKeyPressed = 1;
return false;
case 33:
case 34: //page-up, page-down
if(event.keyCode == 33){
nextRowIndex = rowIndex - pageSize;
if(nextRowIndex < 0) nextRowIndex = 0;
}
if(event.keyCode == 34){
nextRowIndex = rowIndex + pageSize;
if(nextRowIndex > numRows) nextRowIndex = numRows;
}
targetElem = currRow.parent().children("tr").eq(nextRowIndex).children("td").eq(cellIndex)
.find("input[type=text]");
if(targetElem != null){targetElem.select();}
return false;
case 37: //left
if(!(cellIndex == 0)){
targetElem = currRow.children("td").eq(cellIndex-1).find("input[type=text]");
if(targetElem != null){targetElem.select();}}
return false;
case 38: //up
if(rowIndex != 0){
targetElem = currRow.parent().children("tr").eq(rowIndex-1).children("td").eq(cellIndex)
.find("input[type=text]");
if(targetElem != null){
targetElem.select();}}
return false;
case 39: //right arrow
if(!(cellIndex == (numElements -1))){
targetElem = currRow.children("td").eq(cellIndex+1).find("input[type=text]");
if(targetElem != null){targetElem.select();}}
return false;
case 40: //down
if(rowIndex >= 0){
targetElem = currRow.parent().children("tr").eq(rowIndex+1).children("td").eq(cellIndex)
.find("input[type=text]");
if(targetElem != null){targetElem.select();}}
return false;
}
}