我一直在使用 jeresig's hotkey 的 jquery 热键插件。当文档处于焦点时,快捷方式可以正常工作,但是当焦点位于输入字段时,快捷方式不起作用。我使用 $(document)
或 $(document).find('input')
进行绑定(bind)。但这些也不起作用。
我使用以下代码来制作快捷方式:
$(document).ready(function(){
shortcutsInit();
});
function shortcutsInit(){
$(document).bind('keydown', "shift+f2", function() {
window.location.replace("/list");
return false;
});
$(document).bind('keydown', "f3", function() {
if($('#searchholder').length){
$('#searchholder').focus();
}
console.log('f3 pressed');
return false;
});
}
最佳答案
尝试一下:
$(document).ready(function(){
$(document).on("keydown", function(e){
if(e.shiftKey && (e.which || e.keyCode || e.charCode) == 113){
window.location.replace("/list");
return false;
}
if((e.which || e.keyCode || e.charCode) == 114){
if($('#searchholder').length)
$('#searchholder').focus();
console.log('f3 pressed');
return false;
}
});
});
关于javascript - 文档 keydown 绑定(bind)不适用于输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26272806/