当前位置: 动力学知识库 > 问答 > 编程问答 >

jquery - Keypress() not working in IE 9

问题描述:

My jQuery code not working in IE 9 but work well in IE7, IE8, Chrome, FF.

This is my code:

$(document).keypress(function(){

window.clearTimeout();

window.setTimeout(function() {

//Do something...

return false;

}, 800);

});

网友答案:

Remove window.clearTimeout() or give it a parameter. IE9 incorrectly requires it.

$(document).keypress(function(){
    //window.clearTimeout();
    window.setTimeout(function() {
        alert("Worky!");
        return false;
    }, 800);
});

http://jsfiddle.net/Hztuv/1/

or

var timeout;
$(document).keypress(function(){
    window.clearTimeout(timeout);
    timeout = window.setTimeout(function() {
        alert("Worky!");
        return false;
    }, 800);
});
网友答案:

Try switching to keydown, which is different to the computer, but should achieve the same effect

From a tutorial:

In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button. In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.

网友答案:

according to this is better to use keydown() instead of keypress() on IE

分享给朋友:
您可能感兴趣的文章:
随机阅读: