捕获“删除"消息. jQuery按键 [英] Capturing "Delete" Keypress with jQuery

查看:63
本文介绍了捕获“删除"消息. 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事件,而应使用keyupkeydown事件,因为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆