jQuery's 'keypress' doesn't work for some keys in Chrome. How to work around?

后端 未结 6 826
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 12:20

I\'m trying to implement key-press functionality which will remove a div when the user hits Esc. This works for Firefox & IE with the following code:

<
6条回答
  •  孤街浪徒
    2020-12-13 12:32

    For ESC key:

    $(document).keydown(function(e) {
      if(e.keyCode == 27) { /* Run code */ }
    }
    

    For letter keys, like 'L':

    $(document).keypress(function(e) {
      if(e.which == 108) { }
    });
    

    Works in both Chrome and Firefox

提交回复
热议问题