开发者

Key schortcut in JavaScript?

开发者 https://www.devze.com 2023-02-18 05:51 出处:网络
I am trying to make 开发者_JS百科a simple shortcut feature for my site and I am very new in JavaScript. Basically simple thing: when somebody presses n-key (keycode:78) jump to page "display.php&

I am trying to make 开发者_JS百科a simple shortcut feature for my site and I am very new in JavaScript. Basically simple thing: when somebody presses n-key (keycode:78) jump to page "display.php", and I want this to work on all platforms (IE, Mozilla etc.).


Use this:

<script type="text/javascript">
    document.onkeyup = KeyCheck;       

    function KeyCheck(e)
    {
       var KeyID = (window.event) ? event.keyCode : e.keyCode;
       switch(KeyID)
       {
          case KEY_CODE:
          document.Form1.KeyName.value = "...";
          <!-- DO STUFF --> 
       }
    }
 </script>


To ensure cross-browser functionality it is a good idea to rely on common libraries like jQuery. There's a method to do this: http://api.jquery.com/keypress/

Try this

$('body').keypress(function(event) {
  if (event.which == '78') {
     window.location.href = 'yourURL';
   }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消