can't fire keypress event when press delete key

坚强是说给别人听的谎言 提交于 2019-12-21 03:32:09

问题


I'm finding that the delete key doesn't fire the keypress event in Chrome, while other keys work. The problem doesn't occur in Firefox, just in chrome, why? Here is my code:

document.addEventListener('keypress', function (e) {
     console.log(e);
}, false);

回答1:


Use keydown or keyup instead, it captures the delete key (as well as others that keypress doesn't, see http://www.quirksmode.org/js/keys.html)

document.addEventListener('keydown', function (e) {
     console.log(e);
}, false);



回答2:


keypress event for (Del, End, Home,etc..) is not fired in IE, Chrome and safari.. it only works in firefox..

so you can use the keyup or keydown event because the keypress event is intented for real (printable) characters. "keydown" is handled at a lower level so it will capture all non-printing keys like DEL, End, etc



来源:https://stackoverflow.com/questions/10187431/cant-fire-keypress-event-when-press-delete-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!