【发布时间】:2013-12-13 18:05:26
【问题描述】:
我有一个简单的脚本,它使用左右箭头移动到下一篇和上一篇博文。
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 时,它也可以工作。聚焦时禁用键盘快捷键的最佳方法是什么?
谢谢!
【问题讨论】:
-
最好的方法是不听文档,而是听keyup的分页链接。
-
你的意思是像下面建议的@Thanh?
nextEl.onkeyup = prevEl.onkeyup = function(e) {...}我真的无法让它工作......必须听文档。
标签: javascript input focus textarea