event.keycode not returning correct values in firefox

后端 未结 5 623
予麋鹿
予麋鹿 2020-12-31 00:24

I am trying the following code for triggering a js method when space-bar is pressed within an input box.

   

  $(\'#j1         


        
5条回答
  •  离开以前
    2020-12-31 01:13

    In non-IE browsers, you want the which or charCode property in a keypress event rather than the keyCode property. The keypress event is for detecting typed characters while keyup and keydown are for detcting physical keys (in those events, keyCode works in every major browser).

    var charCode = (typeof event.which == "number") ? event.which : event.keyCode;
    

    However, jQuery normalizes the which property of keypress events by using code similar to this, so in jQuery you just need

    var charCode = event.which;
    

    For (a lot) more detail about key events, see http://unixpapa.com/js/key.html.

提交回复
热议问题