我想让 onkeydown
事件只触发一次函数。要再次触发该功能,用户必须松开按键并再次按住。
我知道它相当简单,但我是 JS 的新手。此外,我更愿意避免使用 jQuery 或其他库。
还有一件事,这应该适用于 ie 和 firefox。
最佳答案
我很惊讶它没有被提及,还有 event.repeat
:
document.addEventListener('keydown', (e) => {
if (e.repeat) return;
console.log(e.key);
});
每次按键只会触发一次,因为 event.repeat
在按住键后变为 true
。
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key#keyboardevent_sequence
关于Javascript onkeydown 事件只触发一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5353254/