Prevent BACKSPACE from navigating back with jQuery (Like Google's Homepage)

前端 未结 9 967
陌清茗
陌清茗 2020-11-30 22:48

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

9条回答
  •  被撕碎了的回忆
    2020-11-30 23:37

    This is old, but these answers will still allow you to backspace and navigate when focused on a radio button. You will want to filter the input type as well. Credit to all answers above for this:

    $(document).on("keydown", function (e) {
        if (e.which === 8 && !$(e.target).is("input[type='text']:not([readonly]), textarea")) {
            e.preventDefault();
        }
    });
    

提交回复
热议问题