简体   繁体   English

jQuery keypress()事件没有触发?

[英]jQuery keypress() event not firing?

I am trying to fire an event on the right and left arrow key presses with jQuery. 我试图用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. 我正在为IE用户开发该网站,因为它是一个业务线应用程序。 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 doesn't work in IE try e.keyCode , also you probably want to use keydown() instead of keypress() if you are targeting IE. e.which在IE中不起作用尝试e.keyCode ,如果你的目标是IE,你可能也想使用keydown()而不是keypress ()。

See http://unixpapa.com/js/key.html for more information. 有关更多信息,请参见http://unixpapa.com/js/key.html

With jQuery, I've done it this way : 使用jQuery,我已经做到了这种方式

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

if ($.browser.mozilla) {
    $(document).keypress (checkKey);
} else {
    $(document).keydown (checkKey);
}

Also, try these plugins, which looks like they do all that work for you: 另外,试试这些插件,看起来它们可以为您完成所有工作:

http://www.openjs.com/scripts/events/keyboard_shortcuts http://www.openjs.com/scripts/events/keyboard_shortcuts

http://www.webappers.com/2008/07/31/bind-a-hot-key-combination-with-jquery-hotkeys/ http://www.webappers.com/2008/07/31/bind-a-hot-key-combination-with-jquery-hotkeys/

You have the word 'document' in a string. 你在字符串中有'document'这个词。 Change: 更改:

$('document').keypress(function(e){

to

$(document).keypress(function(e){

Ofcourse this is a closed issue, i would like to add something to your discussion 当然这是一个封闭的问题,我想在你的讨论中加入一些内容

In mozilla i have observed a weird behaviour for this code 在mozilla中,我观察到了这段代码的奇怪行为

$(document).keydown(function(){
//my code 
});

the code is being triggered twice. 代码被触发两次。 When debugged i found that actually there are two events getting fired: 'keypress' and 'keydown'. 在调试时,我发现实际上有两个事件被触发:'keypress'和'keydown'。 I disabled one of the event and the code shown me expected behavior. 我禁用了其中一个事件,代码显示了我预期的行为。

$(document).unbind('keypress');
$(document).keydown(function(){
//my code
});

This works for all browsers and also there is no need to check for browser specific(if($.browser.mozilla){ }). 这适用于所有浏览器,也无需检查特定于浏览器(if($。browser.mozilla){})。

Hope this might be useful for someone 希望这可能对某人有用

您的原始代码有$('document')...它应该有$(文档)没有引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM