阻止BACKSPACE使用jQuery导航(如Google的主页) [英] Prevent BACKSPACE from navigating back with jQuery (Like Google's Homepage)

查看:107
本文介绍了阻止BACKSPACE使用jQuery导航(如Google的主页)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,在Google的主页上,不关注任何元素,按BACKSPACE会将焦点放入搜索工具栏而不是导航回来。

Notice while on Google's homepage, with no focus on any element, pressing BACKSPACE will put the focus into the search toolbar instead of navigating back.

我如何才能完成这个?

How can I accomplish this?

我的应用程序中的用户一直遇到此问题。他们没有专注于任何元素并点击BACKSPACE将它们抛出应用程序。

I keep running into this problem with users in my app. They don't have focus on any element and hit BACKSPACE which throws them out of the app.

推荐答案

我会绑定一个事件处理程序到 keydown 并阻止该事件的默认操作,如果我们处理 textarea 之外的退格键或输入

I would bind an event handler to keydown and prevent the default action of that event if we're dealing with the backspace key outside of a textarea or input:

$(document).on("keydown", function (e) {
    if (e.which === 8 && !$(e.target).is("input, textarea")) {
        e.preventDefault();
    }
});

这篇关于阻止BACKSPACE使用jQuery导航(如Google的主页)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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