开发者

Why won't keyup() and keydown() play nicely together?

I can't get the jQuery keyup and keydown events to work together. See this jsFiddle: http://jsfiddle.net开发者_开发百科/CXkam/1/

Code here too for ease:

$(document).keyup(function (event) {
    alert('Keyup');
});
$(document).keypress(function(e) {
    alert('Keypress: ' + String.fromCharCode(e.which));
});
$(document).keydown(function(e) {
    alert('Keydown: ' + String.fromCharCode(e.which));
});

If you comment out the keypress() and keydown() handlers, then the keyup() alert fires.

But if you don't, then keyup() never fires.

Why not?

Thanks!


The .keyup and .keypress events won't fire if you release the key while the dialogue box is up because the keyup message is sent to the dialogue box, which, for reasons beyond my very limited understanding of things, is not considered part of the document. What you can do, if you want to see it "work," is hold a key down (let's go with T!), hit spacebar to dismiss the dialogue box, and then release the T key. The .keyup message will then be sent to the document and processed as intended.

If you get rid of the .keydown and .keypress functions -- or the alerts within them -- then .keyup will work just fine.


hmm, I think it's actually to do with a problem with alert(). Maybe it's being called too fast or something. Because if you replace it with console.log calls, you can clearly see it's working

http://jsfiddle.net/WJsV6/ Try it in FF. All 3 methods actually fire

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜