jQuery.Excel, 使用Ctrl+方向键/Home/End在input表格中移动

// Enable user to move focus in a grid of inputs with Ctrl + Up, Down, Left, Right, Home, End
// Could be called again after more rows are created.
// deerchao@gmail.com 2009-2-23
// usage:
//--------------Html----------------
// <tr class="dataRow">
// <td><input /></td>
// <td><input /></td>
// <td><input /></td>
// </tr>
// <tr class="dataRow">
// <td><input /></td>
// <td><input /></td>
// <td><input /></td>
// </tr>
//--------------Script-------------
// jQuery.excel('dataRow');
jQuery.extend({
excel: function(rowClass) {
var keys = { left: 37, up: 38, right: 39, down: 40, home: 36, end: 35 };
rowClass = rowClass ? rowClass : '.excel';
if (rowClass[0] != '.')
rowClass = '.' + rowClass;
$(rowClass).unbind('keyup',onkeyup).bind('keyup',onkeyup);
function onkeyup(evt) {
var ctrlOnly = evt.ctrlKey && !evt.altKey && !evt.shiftKey;
switch (evt.keyCode) {
case keys.down:
go("down");
break;
case keys.up:
go("up");
break;
case keys.left:
if (ctrlOnly)
go("left");
break;
case keys.right:
if (ctrlOnly)
go("right");
break;
case keys.home:
if (ctrlOnly)
go("home");
break;
case keys.end:
if (ctrlOnly)
go("end");
break;
}
function go(to) {
var td = $(evt.target).closest('td');
var tr = $(evt.currentTarget);
var toFocus = null;
switch (to) {
case 'home':
toFocus = lastInput(td.prevAll('td'));
break;
case 'end':
toFocus = lastInput(td.nextAll('td'));
break;
case 'left':
toFocus = firstInput(td.prevAll('td'));
if (!toFocus)
toFocus = lastInput(tr.prev('tr' + rowClass).children('td'));
break;
case 'right':
toFocus = firstInput(td.nextAll('td'));
if (!toFocus)
toFocus = firstInput(tr.next('tr' + rowClass).children('td'));
break;
case 'up':
toFocus = firstInput(tr.prev('tr' + rowClass).children('td'), td.prevAll('td').size());
break;
case 'down':
toFocus = firstInput(tr.next('tr' + rowClass).children('td'), td.prevAll('td').size());
break;
}
if (toFocus) {
toFocus.focus();
}
}
function firstInput(tds, start) {
if (!start)
start = 0;
for (var i = start; i < tds.size(); i++) {
var inputs = $(tds[i]).children('input, select, textarea');
if (inputs.size())
return inputs[0];
}
return null;
}
function lastInput(tds) {
for (var i = tds.size() - 1; i >= 0; i--) {
var inputs = $(tds[i]).children('input, select, textarea');
if (inputs.size())
return inputs[0];
}
return null;
}
}
}
});
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2008-02-23 A new way to perform parameter validation in C# 3.0