捕获“删除"消息. jQuery按键 [英] Capturing "Delete" Keypress with jQuery
问题描述
当使用jQuery文档中的示例代码作为keypress事件处理程序时,我无法捕获 Delete 键.在FireFox中按下 Delete 键时,下面的代码片段将记录0
:
When using the example code from the jQuery documentation for the keypress event handler, I'm unable to capture the Delete key. The snippet below is going to log 0
when the Delete key is pressed in FireFox:
$(document).keypress(function(e) {
console.log(e.which);
});
似乎有一种方法可以捕获 Delete 键,但这是一个模棱两可的术语,因此Google并没有提供太多帮助.
Seems there's gotta be a way to capture the Delete key, but it's an ambiguous term so Google isn't proving to be much help with it.
推荐答案
您不应使用keypress
事件,而应使用keyup
或keydown
事件,因为keypress
事件是用于实际的(可打印的) ) 人物. keydown
的处理级别较低,因此它将捕获所有非打印键,例如 delete 和 enter .
You shouldn't use the keypress
event, but the keyup
or keydown
event because the keypress
event is intended for real (printable) characters. keydown
is handled at a lower level so it will capture all nonprinting keys like delete and enter.
这篇关于捕获“删除"消息. jQuery按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!