繁体   English   中英

当文本字段或文本区域使用纯JavaScript成为焦点时,禁用键盘快捷键

[英]Disable keyboard shortcuts when text field or textarea has focus using pure JavaScript

我有一个简单的脚本,该脚本使用左右箭头移至下一个和上一个博客文章。

var nextEl = document.getElementById("pagination__next__link");
var prevEl = document.getElementById("pagination__prev__link");

document.onkeyup = function(e) {

    if (nextEl !== null) {
        if (e.keyCode === 37) {
            window.location.href = nextEl.getAttribute('href');
        }
    }
    if (prevEl !== null) {
        if (e.keyCode === 39) {
            window.location.href = prevEl.getAttribute('href');
        }
    }
    return false;
};

但是,当我将文本inputtextarea焦点时,它也可以工作。 专注时禁用键盘快捷键的最佳方法是什么?

谢谢!

禁止将事件传播到文档

nextEl.onkeyup = prevEl.onkeyup = function(e){ e.stopPropagation(); };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM