使用纯JavaScript禁用文本字段或文本区域具有焦点时的键盘快捷键

使用纯JavaScript禁用文本字段或文本区域具有焦点时的键盘快捷键,javascript,input,focus,textarea,Javascript,Input,Focus,Textarea,我有一个简单的脚本,它使用左箭头和右箭头移动到下一个和上一个blogpost var nextEl = document.getElementById("pagination__next__link"); var prevEl = document.getElementById("pagination__prev__link"); document.onkeyup = function(e) { if (nextEl !== null) { if (e.keyCode

我有一个简单的脚本,它使用左箭头和右箭头移动到下一个和上一个blogpost

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;
};
但是当我有文本
input
textarea
焦点时,它也可以工作。聚焦时禁用键盘快捷键的最佳方式是什么


谢谢

禁用文档输入的事件传播

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

最好的方法是不要听文档,而是听键盘的分页链接。你的意思是像下面的@Thanh建议的那样
nextEl.onkeyup=prevEl.onkeyup=function(e){…}
。。。我必须听文件。