Keyboard navigation using jQuery
I'm trying to add some keyboard support for a website I'm working on and I found these plugins for jQuery: shortKeys and jquery.hotkeys (can't post link because i'm a new user). The thing is I want to make it so that when a user presses "j", f开发者_如何学Pythonor example, to go to a different page, "about.html", for example, but, I don't know how to make that happen. Any suggestions?
You can use window.location
in conjunction with either plugin, like this for shortKeys:
$(document).shortkeys({
'J': function () { window.location = 'about.html'; },
'K': function () { window.location = 'somethingElse.html'; }
});
Or, using Hotkeys:
$(document).bind('keydown', 'j', function() {
window.location = 'about.html';
});
You don't need any jquery plugin for this purpose, following piece of code should suffice:
$( document ).keydown(function(event)
{
switch(event.which)
{
case 74: // 74 is keycode for j
window.location = 'somewhere.html';
break;
case 75: // 75 is keycode for k
window.location = 'another.html';
break;
}
});
You can find keycodes of all keys here http://www.cambiaresearch.com/c4/702b8cd1-e5b0-42e6-83ac-25f0306e3e25/Javascript-Char-Codes-Key-Codes.aspx
精彩评论