使用javascript捕获文档级按键事件 [英] capturing document level keypress events with javascript

查看:82
本文介绍了使用javascript捕获文档级按键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试捕获网页中的文档级按键事件,但是

i've been trying to capture document level key press events in a webpage but

$(document).bind('keydown', 'a', keyevent_cb);

在Firefox中无法始终如一地做出响应.在IE中效果很好(这是一次旅行).有什么建议吗?我尝试了没有jquery的其他解决方案,但它们也无法使用Firefox.

fails to respond consistently in firefox. works great in IE (which is kind of a trip). any recommendations? i've attempted other solutions without jquery and they also fail for firefox.

因此,我愿意接受任何始终如一的结果(无论是否使用jquery).预先感谢.

so i'm open to any result that works consistently (jquery or not). thanks in advance.

推荐答案

以下内容将keypress事件侦听器附加到body元素:

The following attaches a keypress event listener to the body element:

$("body").on("keypress", function (e) {
    // logic for key event here
});

通过您的keyevent_cb回调,您可以轻松做到:

With your keyevent_cb callback, you could simply do:

$("body").on("keypress", keyevent_cb);

这篇关于使用javascript捕获文档级按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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