Catching Backspace on Chrome & Firefox is Different

偶尔善良 提交于 2019-12-07 07:43:26

问题


I am trying to make a console like application, so I am catching all the keypress on the window and do related stuff with them(not important). Problem is at backspace. I have the following code:

$(window).bind("keypress",function(e){
        var code = e.keyCode || e.which;
        if ( code == 8) {
            a = $("#console").html();
            $("#console").html(a.substring(0,a.length-1));
            currentCommand = currentCommand.substring(0,currentCommand.length-1);           
            e.preventDefault();
        }

However, in Firefox, contents of the #console is deleted but Chrome does not execute the code above. I need a cross-browser compatible solution. What am I missing?

ADDITION:

If I use keydown/keyup instead of keypress, I am unable to detect if the characeter was 'A' or 'a' it always returns 'A'.


回答1:


Read this. IE doesn't fire keypress for those special keys. Perhaps it's the same with some of the other browsers.

Javascript e.keyCode doesn't catch Backspace/Del in IE



来源:https://stackoverflow.com/questions/8569138/catching-backspace-on-chrome-firefox-is-different

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