如何在没有焦点的情况下使用javascript读取html页面中的输入 [英] How to read input in a html page with javascript without focus in one field

查看:112
本文介绍了如何在没有焦点的情况下使用javascript读取html页面中的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个人如何在不关注特定领域的情况下阅读html页面中的按键.是否有一些事件可以挂接读取输入的方法?

I want to ask how can someone read keystrokes while in a html page without having focus in a particular field. Is there some event that i can hook a method that reads input?

谢谢.

PS.如果有帮助,我们可以使用JQuery.

PS. We use JQuery if that helps.

推荐答案

使用document.onkeypress(或兄弟姐妹onkeydownonkeyup):

Use document.onkeypress (or the siblings onkeydown and onkeyup):

document.onkeypress = function (event) { ... };

jQuery 为这些事件提供的服务,例如

jQuery offers for these events, e.g.,

$(document).keypress (function (event) { ... });

这将捕获您网站上的所有个按键事件.在函数内部,您可以使用event.keyCode获取所按键的数字代码.请注意,存在一些浏览器不兼容性,尤其是与keypress事件有关.有关FF如何处理的信息,请参见 developer.mozilla.com .

This will catch all key press events on your site. Inside the function you can get the numeric code of the pressed key with event.keyCode. Be aware, that there are some browser incompatibilities, especially with the keypress event. See developer.mozilla.com for how FF handles it.

干杯

这篇关于如何在没有焦点的情况下使用javascript读取html页面中的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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