KeyboardJS

在JS / jQuery中绑定箭头键

不打扰是莪最后的温柔 提交于 2020-02-27 04:17:45
如何在Javascript和/或jQuery中将功能绑定到左右箭头键? 我看了一下jQuery的js-hotkey插件(包装了内置的bind函数来添加一个可识别特定键的参数),但是它似乎不支持箭头键。 #1楼 我只是结合了其他答案中的最佳方法: $(document).keydown(function(e){ switch(e.which) { case $.ui.keyCode.LEFT: // your code here break; case $.ui.keyCode.UP: // your code here break; case $.ui.keyCode.RIGHT: // your code here break; case $.ui.keyCode.DOWN: // your code here break; default: return; // allow other keys to be handled } // prevent default action (eg. page moving up/down) // but consider accessibility (eg. user may want to use keys to choose a radio button) e.preventDefault(); }); #2楼