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

查看:32
本文介绍了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天全站免登陆