jQuery keypress()事件没有触发? [英] jQuery keypress() event not firing?

查看:478
本文介绍了jQuery keypress()事件没有触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用jQuery在右箭头键和左箭头键上按下一个事件。使用以下代码,我可以在任何字母数字键上触发事件,但光标键(向上,向下,向左,向右)不会触发任何内容。我主要为IE用户开发该网站,因为它是一个业务线应用程序。我在这里做错了吗?

I am trying to fire an event on the right and left arrow key presses with jQuery. Using the following code, I can fire events on any of the alphanumeric keys, but the cursor keys (up, down, left, right) fire nothing. I am developing the site primarily for IE users because it is a line of business app. Am I doing something wrong here?

$('document').keypress(function(e){
    switch (e.which) {
        case 40:
            alert('down');
            break;
        case 38:
            alert('up');
            break;
        case 37:
            alert('left');
            break;
        case 39:
            alert('right');
            break;
        default:
            alert('???');  
            }      
});


推荐答案

e.which 不在IE中工作尝试 e.keyCode ,如果你的目标是IE,你可能也想使用keydown()而不是keypress()。

e.which doesn't work in IE try e.keyCode, also you probably want to use keydown() instead of keypress() if you are targeting IE.

有关详细信息,请参阅 http://unixpapa.com/js/key.html

See http://unixpapa.com/js/key.html for more information.

这篇关于jQuery keypress()事件没有触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆