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

后端 未结 6 812
伪装坚强ぢ
伪装坚强ぢ 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:34

    use keydown. keypress doesn't work with ESC in Chrome (not sure about other browsers).

    $(newTag).keydown(function(e) {  //keypress did not work with ESC;
        if (event.which == '13') {
          ProfilePage.saveNewTag(search_id, $(newTag).val());
        }
        else if (window.event.which){
          $(newTag).remove();
        }
    }); 
    

提交回复
热议问题