在Chrome和Chrome上捕捉退格Firefox是不同的 [英] Catching Backspace on Chrome & Firefox is Different

查看:243
本文介绍了在Chrome和Chrome上捕捉退格Firefox是不同的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个像应用程序的控制台,所以我抓住了窗口上的所有按键,并与他们做相关的东西(不重要)。问题是在退格。我有以下代码:

$ $ p $ $ $ $ $ $ $ $ $ $(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();




$ b

然而,在Firefox中, #console code>被删除,但Chrome不执行上面的代码,我需要一个跨浏览器兼容的解决方案。我缺少什么?



ADDITION:如果我使用keydown / keyup而不是按键,我无法检测到字符串是'A'还是'a'它总是返回'A '。

解决方案

阅读这个IE浏览器不会触发这些特殊按键的按键,也许和其他的一样BRO wsers。

JavaScript的e.keyCode不赶上Backspace / Del在IE浏览器


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'.

解决方案

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

这篇关于在Chrome和Chrome上捕捉退格Firefox是不同的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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