how to detect an F5 refresh keypress event in jquery?

倖福魔咒の 提交于 2019-12-10 23:57:55

问题


I noticed that jquery has a keypress function. But it seems that it can only detect keypress event of numbers and characters. It cannot detect the F5 keypress event. And What surprises me the most is everyone online says that the keyCode of F5 is 116, But when I use the jquery keypress function, it just shows that the character t has the keyCode of 116(it seems that 116 is the ascii code of lowercase t)! Can somebody give me any idea about this and how to detect the F5 event in javascript or jquery? Thanks so much in advance.


回答1:


The keypress event does not accept function keys(F1-F12). You can try to use keydown event.




回答2:


I don't know what you did wrong in your code, but jQuery does say F5 is 116 and t is 84:

http://jsfiddle.net/Brjb2/1

One possible error is keypress will have different keycode, that's why keydown is more preferred.

        |  T  |  A  |  F5
keydown |  86 |  65 | 116
keypress| 116 |  97 |  -

Also pressing F5 will not trigger keypress because the reload part happens before keypress.



来源:https://stackoverflow.com/questions/13872853/how-to-detect-an-f5-refresh-keypress-event-in-jquery

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