<legend id='ca9cn'><style id='ca9cn'><dir id='ca9cn'><q id='ca9cn'></q></dir></style></legend>

  1. <small id='ca9cn'></small><noframes id='ca9cn'>

      • <bdo id='ca9cn'></bdo><ul id='ca9cn'></ul>

    1. <tfoot id='ca9cn'></tfoot>
      <i id='ca9cn'><tr id='ca9cn'><dt id='ca9cn'><q id='ca9cn'><span id='ca9cn'><b id='ca9cn'><form id='ca9cn'><ins id='ca9cn'></ins><ul id='ca9cn'></ul><sub id='ca9cn'></sub></form><legend id='ca9cn'></legend><bdo id='ca9cn'><pre id='ca9cn'><center id='ca9cn'></center></pre></bdo></b><th id='ca9cn'></th></span></q></dt></tr></i><div id='ca9cn'><tfoot id='ca9cn'></tfoot><dl id='ca9cn'><fieldset id='ca9cn'></fieldset></dl></div>

      使用 jQuery 监听 keydown 事件

      时间:2023-09-04
      • <bdo id='5lrLf'></bdo><ul id='5lrLf'></ul>
        1. <i id='5lrLf'><tr id='5lrLf'><dt id='5lrLf'><q id='5lrLf'><span id='5lrLf'><b id='5lrLf'><form id='5lrLf'><ins id='5lrLf'></ins><ul id='5lrLf'></ul><sub id='5lrLf'></sub></form><legend id='5lrLf'></legend><bdo id='5lrLf'><pre id='5lrLf'><center id='5lrLf'></center></pre></bdo></b><th id='5lrLf'></th></span></q></dt></tr></i><div id='5lrLf'><tfoot id='5lrLf'></tfoot><dl id='5lrLf'><fieldset id='5lrLf'></fieldset></dl></div>

              <tbody id='5lrLf'></tbody>

              <legend id='5lrLf'><style id='5lrLf'><dir id='5lrLf'><q id='5lrLf'></q></dir></style></legend>
              <tfoot id='5lrLf'></tfoot>

              1. <small id='5lrLf'></small><noframes id='5lrLf'>

                本文介绍了使用 jQuery 监听 keydown 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我想检测何时按下回车键,在 HTML 上动态注入.

                I want to detect when the enter key is pressed, on HTML that will be injected dynamically.

                要简单地检测何时按下回车键,我可以这样做:

                To simply detect when the enter key is pressed, I can do:

                $('#textfield').keydown(function (e){
                    if(e.keyCode == 13){
                        console.log('Enter was pressed');
                    }
                })
                

                此代码适用于 on(),但我担心它效率低下,因为 jQuery 会在每次按下键时进行检查.这有什么低效的地方吗?

                This code works for on(), but I am worried it is inefficient since jQuery will check every time a key is pressed. Is there anything inefficient about this?

                $('body').on('keydown','#textfield', function(event) {
                  if (event.keyCode == 13) {
                    console.log('Enter was pressed');
                  }
                }
                

                推荐答案

                如果你想捕捉页面任意位置的按键 -

                If you want to capture the keypress anywhere on the page -

                $(document).keypress(function(e) {
                  if(e.which == 13) {
                    // enter pressed
                  }
                });
                

                不用担心它会检查每个按键,它确实不会给浏览器带来任何显着负载.

                Don't worry about the fact this checks for every keypress, it really isn't putting any significant load on the browser.

                这篇关于使用 jQuery 监听 keydown 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 Javascript 中访问事件处理程序中的类成员变量 下一篇:在 jQuery 中绑定现有的 JavaScript 函数

                相关文章

                最新文章

              2. <legend id='pTP8t'><style id='pTP8t'><dir id='pTP8t'><q id='pTP8t'></q></dir></style></legend>

              3. <tfoot id='pTP8t'></tfoot>
                  <bdo id='pTP8t'></bdo><ul id='pTP8t'></ul>
                1. <small id='pTP8t'></small><noframes id='pTP8t'>

                    <i id='pTP8t'><tr id='pTP8t'><dt id='pTP8t'><q id='pTP8t'><span id='pTP8t'><b id='pTP8t'><form id='pTP8t'><ins id='pTP8t'></ins><ul id='pTP8t'></ul><sub id='pTP8t'></sub></form><legend id='pTP8t'></legend><bdo id='pTP8t'><pre id='pTP8t'><center id='pTP8t'></center></pre></bdo></b><th id='pTP8t'></th></span></q></dt></tr></i><div id='pTP8t'><tfoot id='pTP8t'></tfoot><dl id='pTP8t'><fieldset id='pTP8t'></fieldset></dl></div>