【问题标题】:Redirect on Key Press? [duplicate]按键重定向? [复制]
【发布时间】:2016-10-11 19:57:50
【问题描述】:

所以我有一个网站,我正在寻找一种在用户按下 Esc 键时将其重定向到另一个网页的方法。

【问题讨论】:

    标签: javascript html keyboard-shortcuts


    【解决方案1】:

    您可以使用 keydown 事件并捕获 event.key,如果 event.key === "Escape",则发出重定向。像这样:

    document.body.addEventListener("keydown", function (event) {
        if (event.key === "Escape") {
            window.location.replace("/*your url here*/");
        }
    });
    

    【讨论】:

      【解决方案2】:

      对 ESC 键使用键码 27。有关更多详细信息,请参阅以下答案。 How to detect escape key press with JavaScript or jQuery?

      【讨论】:

        猜你喜欢
        • 2012-11-13
        • 1970-01-01
        • 1970-01-01
        • 2014-05-16
        • 2022-06-15
        • 2015-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多