<bdo id='2N7Al'></bdo><ul id='2N7Al'></ul>
<legend id='2N7Al'><style id='2N7Al'><dir id='2N7Al'><q id='2N7Al'></q></dir></style></legend>

    1. <small id='2N7Al'></small><noframes id='2N7Al'>

      <tfoot id='2N7Al'></tfoot>

        <i id='2N7Al'><tr id='2N7Al'><dt id='2N7Al'><q id='2N7Al'><span id='2N7Al'><b id='2N7Al'><form id='2N7Al'><ins id='2N7Al'></ins><ul id='2N7Al'></ul><sub id='2N7Al'></sub></form><legend id='2N7Al'></legend><bdo id='2N7Al'><pre id='2N7Al'><center id='2N7Al'></center></pre></bdo></b><th id='2N7Al'></th></span></q></dt></tr></i><div id='2N7Al'><tfoot id='2N7Al'></tfoot><dl id='2N7Al'><fieldset id='2N7Al'></fieldset></dl></div>
      1. 如何将 JavaScript 键盘快捷键添加到现有 JavaScrip

        时间:2023-09-07
        <i id='3rA0C'><tr id='3rA0C'><dt id='3rA0C'><q id='3rA0C'><span id='3rA0C'><b id='3rA0C'><form id='3rA0C'><ins id='3rA0C'></ins><ul id='3rA0C'></ul><sub id='3rA0C'></sub></form><legend id='3rA0C'></legend><bdo id='3rA0C'><pre id='3rA0C'><center id='3rA0C'></center></pre></bdo></b><th id='3rA0C'></th></span></q></dt></tr></i><div id='3rA0C'><tfoot id='3rA0C'></tfoot><dl id='3rA0C'><fieldset id='3rA0C'></fieldset></dl></div>

        <small id='3rA0C'></small><noframes id='3rA0C'>

        <legend id='3rA0C'><style id='3rA0C'><dir id='3rA0C'><q id='3rA0C'></q></dir></style></legend>
        • <tfoot id='3rA0C'></tfoot>

          • <bdo id='3rA0C'></bdo><ul id='3rA0C'></ul>

                    <tbody id='3rA0C'></tbody>
                  本文介绍了如何将 JavaScript 键盘快捷键添加到现有 JavaScript 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是我的代码:

                  function pauseSound() {
                      var pauseSound = document.getElementById("backgroundMusic");
                      pauseSound.pause(); 
                  }
                  

                  我想为这段代码添加一个键盘快捷键,我该如何做,以便在单击按钮时也可以执行该功能?

                  I would like to add a keyboard shortcut to this code, how can I do this so that the function can also be executed when a button is clicked too?

                  尝试添加一个 else if 语句,但它不起作用,有什么想法吗?

                  Tried to add an else if statement but it doesn't work, any ideas?

                  function doc_keyUp(e) {
                      if (e.ctrlKey && e.keyCode == 88) {
                          pauseSound();
                      }
                  
                      else if (e.ctrlKey && e.keyCode == 84) {
                          playSound();
                      }
                  }
                  

                  推荐答案

                  文档的 keyup 事件的事件处理程序似乎是一个合适的解决方案.

                  An event handler for the document's keyup event seems like an appropriate solution.

                  注意:KeyboardEvent.keyCode 已被弃用,取而代之的是 key.

                  Note: KeyboardEvent.keyCode was deprecated in favor of key.

                  // define a handler
                  function doc_keyUp(e) {
                  
                      // this would test for whichever key is 40 (down arrow) and the ctrl key at the same time
                      if (e.ctrlKey && e.key === 'ArrowDown') {
                          // call your function to do the thing
                          pauseSound();
                      }
                  }
                  // register the handler 
                  document.addEventListener('keyup', doc_keyUp, false);
                  

                  这篇关于如何将 JavaScript 键盘快捷键添加到现有 JavaScript 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 JavaScript 和 jQuery,跨浏览器处理按键事件 下一篇:keypress 和 keyup - 为什么 keyCode 不同?

                  相关文章

                  最新文章

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

                  1. <legend id='5arch'><style id='5arch'><dir id='5arch'><q id='5arch'></q></dir></style></legend>
                    1. <tfoot id='5arch'></tfoot>
                        <bdo id='5arch'></bdo><ul id='5arch'></ul>
                    2. <small id='5arch'></small><noframes id='5arch'>