keydown 和 keyup 事件
keydown 事件发生在键盘上的任意键被按下时。
与按键不同,所有按键都会触发该事件。
keyup 事件在释放键时发生。
keydown 和 keyup 事件为我们提供一个代码,告诉我们按下了哪个键,而 keypress 则告诉我们输入了哪个字符。
keyup 和 keydown 事件处理程序用于检测转义键按下。
当在键盘上按下转义键时,事件处理程序会在文档上触发。
<!DOCTYPE html> <html> <head> <title>文档的标题</title> <script src="https://code.jquery.com/jquery-3.5.0.js"></script> </head> <body> <div>Please press the Esc key</div> <script> $(document).on( 'keydown', function(event) { if(event.key == "Escape") { alert('Esc key pressed.'); } }); </script> </body> </html>
我们还可以使用 keyup 事件处理程序来检测转义键按下:
<!DOCTYPE html> <html> <head> <title>文档的标题</title> <script src="https://code.jquery.com/jquery-3.5.0.js"></script> </head> <body> <div>Please press the Esc key</div> <script> $(document).on('keyup', function(event) { if(event.key == "Escape") { alert('Esc key pressed.'); } }); </script> </body> </html>
日期:2020-06-02 22:16:16 来源:oir作者:oir