本文介绍了Javascript e.keyCode没有在IE中捕获Backspace / Del的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图用这种代码使用javascript / jQuery来捕捉Backspace和Delete键的紧急事件。

I'm trying to catch the pressing event of Backspace and Delete keys using javascript/jQuery with this kind of code.

$("textarea[name=txt]").keypress(function(e){
    var keycode =  e.keyCode ? e.keyCode : e.which;

    if(keycode == 8){ // backspace
        // do somethiing
        alert(keycode);
    }

    if(keycode == 46){ // delete
        // do somethiing
        alert(keycode);
    } 

});

这些代码行在Firefox(3.6.12)中完美运行。这意味着当按下Backspace或Delete时会弹出警报。
但这不适用于Internet Explorer(8)

These lines of code works perfectly in Firefox (3.6.12). That means the alert is popped up when Backspace or Delete is pressed.But this is not working in Internet Explorer (8)

有人可以建议我采用不同的方式在Internet Explorer中捕获这些关键的新闻事件吗?

Can anyone suggest me a different way to catch these key press events in Internet Explorer?

推荐答案

如果你想支持IE并且你使用特殊键(比如 delete 退格)我建议改用 keydown / keyup 。 / p>

If you want to support IE and you use special keys (like delete and backspace) I suggest using keydown/keyup instead.

资源管理器不触发按键
事件,删除,结束,输入,转义,
功能键,主页,插入,
pageUp / Down和tab。

Explorer doesn't fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab.

如果您需要检测这些密钥,请帮忙并搜索他们的keyCode onkeydown / up ,并忽略onkeypress和charCode。

If you need to detect these keys, do yourself a favour and search for their keyCode onkeydown/up, and ignore both onkeypress and charCode.

您可以阅读有关 (Quirksmode)

You can read more on cross browser issues of Detecting keystrokes (Quirksmode).

这篇关于Javascript e.keyCode没有在IE中捕获Backspace / Del的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 17:46