jQuery的“按键”对Chrome中的一些按键无效。如何解决?

时间:2022-05-19 00:08:11

I'm trying to implement key-press functionality which will remove a div when the user hits Esc. This works for Firefox & IE with the following code:

我正在尝试实现按键功能,当用户点击Esc时,它会删除一个div。这适用于Firefox和IE,代码如下:

$("body").keypress(function(e) {
    alert("any key pressed");
    if (e.keyCode == 27) {
        alert("escape pressed");
    }
});

If I hit any key, the first alert is displayed, and if I hit Escape, the second alert is also displayed.

如果我点击任何键,第一个警报就会显示出来,如果我点击Escape,第二个警报也会显示出来。

This doesn't work with Chrome though. The first alert is always displayed if I hit any of the letter keys, but not when I hit Escape, Tab, Space or any of the numbers.

不过这对Chrome不适用。如果我点击了任何一个字母键,第一个警告就会显示出来,但当我点击Escape、Tab、空格或任何一个数字时就不会显示。

Why would this be? Is there any way to get Chrome to respond to these key presses?

这是为什么呢?有什么方法可以让Chrome对这些按键进行响应吗?

6 个解决方案

#1


110  

Try handling keydown instead.

试着处理keydown代替。

#2


17  

use keydown. keypress doesn't work with ESC in Chrome (not sure about other browsers).

keydown使用。keypress不与ESC在Chrome上合作(不确定其他浏览器)。

$(newTag).keydown(function(e) {  //keypress did not work with ESC;
    if (event.which == '13') {
      ProfilePage.saveNewTag(search_id, $(newTag).val());
    }
    else if (window.event.which){
      $(newTag).remove();
    }
}); 

#3


2  

For ESC key:

ESC键:

$(document).keydown(function(e) {
  if(e.keyCode == 27) { /* Run code */ }
}

For letter keys, like 'L':

字母键,如“L”:

$(document).keypress(function(e) {
  if(e.which == 108) { }
});

Works in both Chrome and Firefox

适用于Chrome和Firefox

#4


1  

After the second alert add also

第二个警报之后也添加

e.preventDefault();

This will prevent the default action of the event to be triggered.

这将防止触发事件的默认操作。

More info about this method here

更多有关此方法的信息

Your code should look like

您的代码应该是这样的

$("body").keypress(function(e) {
    alert("any key pressed");
    if (e.keyCode == 27) {
         alert("escape pressed");
         e.preventDefault();
}});

#5


0  

Using Jquery.hotkey js file you can Make Sortcut key

使用Jquery。hotkey js文件,你可以制作Sortcut键

$(document).bind('keydown', 'esc', function(){ });

#6


0  

keypress 'ESC'

键盘按键“ESC”

e.which: 0
e.keyCode: 27

keyup 'ESC'

keyup“ESC”

e.which: 27
e.keyCode: 27

For non-printable characters better use keyup.

对于不可打印字符,最好使用keyup。

#1


110  

Try handling keydown instead.

试着处理keydown代替。

#2


17  

use keydown. keypress doesn't work with ESC in Chrome (not sure about other browsers).

keydown使用。keypress不与ESC在Chrome上合作(不确定其他浏览器)。

$(newTag).keydown(function(e) {  //keypress did not work with ESC;
    if (event.which == '13') {
      ProfilePage.saveNewTag(search_id, $(newTag).val());
    }
    else if (window.event.which){
      $(newTag).remove();
    }
}); 

#3


2  

For ESC key:

ESC键:

$(document).keydown(function(e) {
  if(e.keyCode == 27) { /* Run code */ }
}

For letter keys, like 'L':

字母键,如“L”:

$(document).keypress(function(e) {
  if(e.which == 108) { }
});

Works in both Chrome and Firefox

适用于Chrome和Firefox

#4


1  

After the second alert add also

第二个警报之后也添加

e.preventDefault();

This will prevent the default action of the event to be triggered.

这将防止触发事件的默认操作。

More info about this method here

更多有关此方法的信息

Your code should look like

您的代码应该是这样的

$("body").keypress(function(e) {
    alert("any key pressed");
    if (e.keyCode == 27) {
         alert("escape pressed");
         e.preventDefault();
}});

#5


0  

Using Jquery.hotkey js file you can Make Sortcut key

使用Jquery。hotkey js文件,你可以制作Sortcut键

$(document).bind('keydown', 'esc', function(){ });

#6


0  

keypress 'ESC'

键盘按键“ESC”

e.which: 0
e.keyCode: 27

keyup 'ESC'

keyup“ESC”

e.which: 27
e.keyCode: 27

For non-printable characters better use keyup.

对于不可打印字符,最好使用keyup。