在 Chrome 和 Chrome 上捕获 Backspace火狐浏览器与众不同
我正在尝试制作一个类似控制台的应用程序,所以我捕获窗口上的所有按键并用它们做相关的事情(不重要)。问题出在退格键上。我有以下代码:
$(window).bind("keypress",function(e){
var code = e.keyCode || e.which;
if ( code == 8) {
a = $("#console").html();
$("#console").html(a.substring(0,a.length-1));
currentCommand = currentCommand.substring(0,currentCommand.length-1);
e.preventDefault();
}
但是,在 Firefox 中,#console
的内容被删除,但 Chrome 不执行上面的代码。我需要一个跨浏览器兼容的解决方案。我缺少什么?
补充:
如果我使用 keydown/keyup 而不是按键,我无法检测字符是“A”还是“a”,它总是返回“A”。
I am trying to make a console like application, so I am catching all the keypress on the window and do related stuff with them(not important). Problem is at backspace. I have the following code:
$(window).bind("keypress",function(e){
var code = e.keyCode || e.which;
if ( code == 8) {
a = $("#console").html();
$("#console").html(a.substring(0,a.length-1));
currentCommand = currentCommand.substring(0,currentCommand.length-1);
e.preventDefault();
}
However, in Firefox, contents of the #console
is deleted but Chrome does not execute the code above. I need a cross-browser compatible solution. What am I missing?
ADDITION:
If I use keydown/keyup instead of keypress, I am unable to detect if the characeter was 'A' or 'a' it always returns 'A'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
读一下这个。 IE 不会触发这些特殊键的按键。也许其他一些浏览器也是如此。
Javascript e.keyCode 在 IE 中无法捕获 Backspace/Del< /a>
Read this. IE doesn't fire keypress for those special keys. Perhaps it's the same with some of the other browsers.
Javascript e.keyCode doesn't catch Backspace/Del in IE