jquery是最小化命令脚本的更好方法
每次我想让用户能够使用键盘进行快捷操作时,我都会包含这个按键命令脚本。但我想知道是否有办法将其最小化或简化,因为在同一个脚本中,它基本上是一次又一次地重复相同的事情。也许是数组什么的jquery是最小化命令脚本的更好方法,jquery,plugins,command,simplify,Jquery,Plugins,Command,Simplify,每次我想让用户能够使用键盘进行快捷操作时,我都会包含这个按键命令脚本。但我想知道是否有办法将其最小化或简化,因为在同一个脚本中,它基本上是一次又一次地重复相同的事情。也许是数组什么的 var isCtrl = false; $(document).keyup(function (e) { if(e.which == 17 || e.which == 91) isCtrl=false; }).keydown(function (e) { // Check if CTRL
var isCtrl = false;
$(document).keyup(function (e) {
if(e.which == 17 || e.which == 91) isCtrl=false;
}).keydown(function (e) {
// Check if CTRL || CMND is hold down
if(e.which == 17 || e.which == 91) isCtrl=true;
// CTRL+S || CMND+S
if(e.which == 83 && isCtrl == true) {
ctrl_s_command();
return false;
}
/** HOTKEYS **/
// ESC Key
if (e.keyCode == 27) {
esc_key_command();
return false;
}
// Enter Key
if (e.keyCode == 13) {
enter_key_command();
return false;
}
// Space Bar
if (e.keyCode == 32) {
space_bar_command();
return false;
}
// Alt Key
if (e.keyCode == 18) {
alt_key_command();
return false;
}
// Tab key
if (e.keyCode == 9) {
tab_key_command();
return false;
}
// Shift key
if (e.keyCode == 16) {
shift_key_command();
return false;
}
// Caps Lock
if (e.keyCode == 20) {
caps_lock_command();
return false;
}
// Backspace key
if (e.keyCode == 8) {
backspace_key_command();
return false;
}
// Home key
if (e.keyCode == 36) {
home_key_command();
return false;
}
// End key
if (e.keyCode == 35) {
end_key_command();
return false;
}
// Delete key
if (e.keyCode == 46) {
delete_key_command();
return false;
}
// Insert key
if (e.keyCode == 45) {
insert_key_command();
return false;
}
// Page Up key
if (e.keyCode == 33) {
page_up_key_command();
return false;
}
// Page Down key
if (e.keyCode == 34) {
page_down_key_command();
return false;
}
// Numlock key
if (e.keyCode == 144) {
numlock_key_command();
return false;
}
// Scroll Lock key
if (e.keyCode == 145) {
scroll_lock_command();
return false;
}
// Pause Break key
if (e.keyCode == 19) {
pause_break_command();
return false;
}
/** ARROWS **/
// Left Arrow Key
if (e.keyCode == 37) {
left_arrow_command();
return false;
}
// Up Arrow Key
if (e.keyCode == 38) {
up_arrow_command();
return false;
}
// Right Arrow Key
if (e.keyCode == 39) {
right_arrow_command();
return false;
}
// Down Arrow Key
if (e.keyCode == 40) {
down_arrow_command();
return false;
}
/** F Keys **/
// F1 Key
if (e.keyCode == 112) {
f1_key_command();
return false;
}
// F2 Key
if (e.keyCode == 113) {
f2_key_command();
return false;
}
// F3 Key
if (e.keyCode == 114) {
f3_key_command();
return false;
}
// F4 Key
if (e.keyCode == 115) {
f4_key_command();
return false;
}
// F5 Key
if (e.keyCode == 116) {
f5_key_command();
return false;
}
// F6 Key
if (e.keyCode == 117) {
f6_key_command();
return false;
}
// F7 Key
if (e.keyCode == 118) {
f7_key_command();
return false;
}
// F8 Key
if (e.keyCode == 119) {
f8_key_command();
return false;
}
// F9 Key
if (e.keyCode == 120) {
f9_key_command();
return false;
}
// F10 Key
if (e.keyCode == 121) {
f10_key_command();
return false;
}
// F11 Key
if (e.keyCode == 122) {
f11_key_command();
return false;
}
// F12 Key
if (e.keyCode == 123) {
f12_key_command();
return false;
}
});
您可以这样声明您的函数:
var keyCommand = {};
// ESC Command
keyCommand[27] = function() { alert("Escape"); };
// Enter Command
keyCommand[13] = function() { alert("Enter"); };
// Tab key Command
keyCommand[9] = function() { alert("Tab"); };
// etc etc
或将其分配给已定义的函数:
var keyCommand = {};
// ESC Command
keyCommand[27] = esc_key_command;
// Enter Command
keyCommand[13] = enter_key_command;
// Tab key Command
keyCommand[9] = tab_key_command;
// etc etc
然后使用
if (typeof keyCommand[e.keyCode] == 'function')
keyCommand[e.keyCode]();
你想使用switch语句吗?你能给我举个例子吗?这就是我所说的:,但是选择的答案采用了更好的方法。我之所以这样做,是因为它可能不是每次键都具有的相同功能。有时,根据
esc\u key\u命令()
,使用esc
键可以关闭窗口或将对象恢复到以前的位置。。还是会有很多声明。。但是我想,如果不是所有的东西都被声明的话,重新设计脚本来工作可能是不可能的。