Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
294 views
in Technique[技术] by (71.8m points)

javascript - 如何使用纯JS或jQuery检测转义键?(How to detect escape key press with pure JS or jQuery?)

Possible Duplicate:(可能重复:)

How to detect escape key press in IE, Firefox and Chrome?(如何在IE,Firefox和Chrome中检测转义按键?)

Below code works in IE and alerts 27 , but in Firefox it alerts 0(下面的代码适用于IE和警报27 ,但在Firefox中它会提醒0)
$('body').keypress(function(e){
    alert(e.which);
    if(e.which == 27){
        // Close my modal window
    }
});
  ask by Mithun Sreedharan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Note: keyCode is getting deprecated use key instead.(注意: keyCode正在被弃用的使用key 。)

function keyPress (e) {
    if(e.key === "Escape") {
        // write your logic here.
    }
}

Here is jsfiddle(这是jsfiddle)


keyCode is Getting deprecated(keyCode已弃用)

It seems keydown and keyup work, even though keypress may not(它似乎是keydownkeyup工作,即使keypress可能没有)


$(document).keyup(function(e) {
     if (e.key === "Escape") { // escape key maps to keycode `27`
        // <DO YOUR WORK HERE>
    }
});

Which keycode for escape key with jQuery(使用jQuery的escape key的哪个键代码)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...