我正在尝试制作一个类似控制台的应用程序,所以我正在捕捉窗口上的所有按键并用它们做相关的事情(不重要)。问题在于退格。我有以下代码:
$(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();
}
但是,在 Firefox 中,内容#console
被删除,但 Chrome 不执行上述代码。我需要一个跨浏览器兼容的解决方案。我错过了什么?
添加:
如果我使用 keydown/keyup 而不是 keypress,我无法检测到字符是“A”还是“a”,它总是返回“A”。