在 Chrome 和 Chrome 上捕获 Backspace火狐浏览器与众不同

发布于 2024-12-21 19:17:58 字数 621 浏览 5 评论 0原文

我正在尝试制作一个类似控制台的应用程序,所以我捕获窗口上的所有按键并用它们做相关的事情(不重要)。问题出在退格键上。我有以下代码:

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝眸 2024-12-28 19:17:58

读一下这个。 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文