为什么 JQuery keydown 适用于窗口而不适用于文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/187229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:45:36  来源:igfitidea点击:

Why does JQuery keydown work for window but not textbox?

jquery

提问by Edward Tanguay

Why does this work:

为什么这样做:

$(window).keydown(function(event){
    alert(event.keyCode);
});

but not this:

但不是这个:

$('#ajaxSearchText').keydown(function(event){
    alert(event.keyCode);
});

I'm testing with Firefox 3. Interestingly, neither of them work in IE7.

我正在使用 Firefox 3 进行测试。有趣的是,它们都不适用于 IE7。

回答by Alexander Prokofyev

Checked this in Chrome, IE7 and Firefox 3.0.3. Works as it should. jQuery version 1.2.6.

在 Chrome、IE7 和 Firefox 3.0.3 中检查了这一点。正常工作。jQuery 版本 1.2.6。

<html> 
  <head> 
    <script type="text/javascript" src="jquery-1.2.6.js"></script> 
    <script type="text/javascript"> 
      $(function() 
      {
        $("#ajaxSearchText").keydown(function(event)
        {
          alert(event.keyCode);
        });
      });
    </script> 
  </head> 
  <body> 
    <input type="text" id="ajaxSearchText"></input>
  </body> 
</html> 

回答by Rik Heywood

To fix the issues in IE6 and IE7, try this...

要解决 IE6 和 IE7 中的问题,请尝试以下操作...

$(function() 
{
    $(document).keydown(function(event){
        alert(event.keyCode);
    });
});

Attaching the event to the $(document)seems to be the magic things here.

将事件附加到这里$(document)似乎是神奇的事情。

Your first bit of code really should work in IE as well though. It seems to be down to a bug in jQuery that will hopefully be fixed soon...

不过,您的第一部分代码确实也应该在 IE 中工作。这似乎归结为 jQuery 中的一个错误,有望很快得到修复......

Here is a link to the bug report in jQuery. https://bugs.jquery.com/ticket/3614

这是 jQuery 中错误报告的链接。https://bugs.jquery.com/ticket/3614

回答by penderi

For all your keydown/keyup/keyboard needs, use the jQuery hotkeys plugin.

对于您所有的 keydown/keyup/keyboard 需求,请使用 jQuery 热键插件。

Saw this a few months ago and it never fails to impress it. Follow the jump for the plugin demo ... http://code.google.com/p/js-hotkeys/

几个月前看到了这个,它永远不会给它留下深刻印象。按照插件演示的跳转... http://code.google.com/p/js-hotkeys/

It maps ALL keys on a keyboard including combos. Hope it helps!

它映射键盘上的所有键,包括组合键。希望能帮助到你!

回答by Mote

Try using

尝试使用

$('#ajaxSearchText').keyup(function(event){
    alert(event.keyCode);
});

works for me perfectly. Also check the id of the textarea

非常适合我。还要检查 textarea 的 id

回答by Kristian

$('#searchInput').keydown(function() {
    alert('testing');
});

will not work. However, if you wrap it in a function:

不管用。但是,如果将其包装在函数中:

$(function()
{
    $('#searchInput').keydown(function() {
        alert('testing');
    });
});

it will work.

它会起作用。

Without that function declaration, it only works on objects like document and window.

没有那个函数声明,它只适用于像文档和窗口这样的对象。

回答by Simon_Weaver

Because key events are not supported on the 'window' object.

因为 'window' 对象不支持按键事件。

http://www.w3schools.com/jsref/obj_window.asp

http://www.w3schools.com/jsref/obj_window.asp

But only on 'document':

但仅限于“文档”:

http://www.w3schools.com/jsref/dom_obj_event.asp

http://www.w3schools.com/jsref/dom_obj_event.asp

If it ever worked then it was a bug in jQuery.

如果它曾经奏效,那么它就是 jQuery 中的一个错误。