문제

$(document).keypress(function(e)
{
  alert(e.keyCode);
  if(e.keyCode==27)
  {
    hide_menu();
  }
});

I get the alert for all keys except the escape key and the success part of the if is never getting called. Why this happens?

도움이 되었습니까?

해결책

use $(document).keyup instead of $(document).keypress

the following code works fine:

$(document).keyup(function(e) 
{
  alert(e.keyCode);

  if(e.keyCode==27)
  {
      alert ("Esc key");
      hide_menu();
  }
});

다른 팁

You can change the keypress to keyup(better to use keyup) or keydown:

$(document).keyup(function(e){
    alert(e.keyCode);
    if(e.keyCode==27){
       hide_menu();
    }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top