Creating a shortcut key for the letter j to redirect to a url using jquery

泄露秘密 提交于 2020-01-05 03:02:36

问题


Using jQuery (if it helps), I'd like to create a shortcut key for the letter J, so when someone clicks on the keyboard letter J, it redirects to a webpage.

Is this possible?


回答1:


The short answer is yes, this is possible. See Jquery Events Keypress

If you want it to be a universal shortcut then simply bind

$(document).keypress(function(event) {
   if (event.which === 106) { window.location = 'your_url'; }
});

Remember to make it very clear to the users that this will happen. There's nothing more off putting to a user than to accidentally trigger a command they didn't know existed.




回答2:


Based on this page here http://docs.jquery.com/Events/keypress

$().keypress(function (e)
{
   //74 == J
   //106 == j
   if (e.which == 74 || e.which == 106)
    {
         //redirect
    }
});



回答3:


Already asked on SO, read this thread:

Keyboard shortcuts with jQuery

I think, that this answer is what you are looking for (it uses a jQuery plugin):

Keyboard shortcuts with jQuery



来源:https://stackoverflow.com/questions/1580814/creating-a-shortcut-key-for-the-letter-j-to-redirect-to-a-url-using-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!