<small id='iEr6S'></small><noframes id='iEr6S'>

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

        <tfoot id='iEr6S'></tfoot>

        <i id='iEr6S'><tr id='iEr6S'><dt id='iEr6S'><q id='iEr6S'><span id='iEr6S'><b id='iEr6S'><form id='iEr6S'><ins id='iEr6S'></ins><ul id='iEr6S'></ul><sub id='iEr6S'></sub></form><legend id='iEr6S'></legend><bdo id='iEr6S'><pre id='iEr6S'><center id='iEr6S'></center></pre></bdo></b><th id='iEr6S'></th></span></q></dt></tr></i><div id='iEr6S'><tfoot id='iEr6S'></tfoot><dl id='iEr6S'><fieldset id='iEr6S'></fieldset></dl></div>
          <bdo id='iEr6S'></bdo><ul id='iEr6S'></ul>
      1. 为什么在 keydown 回调中返回 false 不会停止按钮单

        时间:2023-09-07
            <tbody id='h7ERx'></tbody>
          <legend id='h7ERx'><style id='h7ERx'><dir id='h7ERx'><q id='h7ERx'></q></dir></style></legend>

            <small id='h7ERx'></small><noframes id='h7ERx'>

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

            • <tfoot id='h7ERx'></tfoot>

                  本文介绍了为什么在 keydown 回调中返回 false 不会停止按钮单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个按钮和以下 javascript 例程.

                  I have a button and the following javascript routine.

                  $("button").keydown( function(key) {
                    switch(key.keyCode) {
                    case 32: //space
                      return false;
                    }
                  } );
                  

                  据我了解,return false; 会阻止按键被处理.所以 $("button").click(); 不会被调用.对于其他键码,这按预期工作.例如,如果我截取40,即向下按钮,则页面不滚动.

                  as I understood it, the return false; would stop the keypress from being processed. So $("button").click(); would not be called. For other keyCodes, this works as expected. For example, if I intercept40, which is the down button, the page is not scrolling.

                  我在 Firefox 中注意到了这种行为.

                  I noticed this behavior in Firefox.

                  为什么 return false; 不会停止空间上的按钮单击事件?javascript 规范对此有何评论?

                  Why does the return false; does not stop the button click event on space? What does the javascript spec say about this?

                  推荐答案

                  希望这能回答你的问题:

                  Hope this answers your question:

                  <input type="button" value="Press" onkeydown="doOtherStuff(); return false;">
                  

                  return false; 如果在 HTML 中的事件处理程序属性的末尾调用,则成功取消跨浏览器的事件.据我所知,这种行为在任何地方都没有正式规定.

                  return false; successfully cancels an event across browsers if called at the end of an event handler attribute in the HTML. This behaviour is not formally specified anywhere as far as I know.

                  如果您改为通过 DOM 元素上的事件处理程序属性设置事件(例如 button.onkeydown = function(evt) {...})或使用 addEventListener/attachEvent(例如 button.addEventListener("keydown", function(evt) {...}, false))然后只返回 false 来自该函数的功能并非在每个浏览器中都有效,您需要从我的其他答案中执行 returnValuepreventDefault() 内容.preventDefault 在 DOM 2 规范 并由大多数主流现代浏览器实现.returnValue 是特定于 IE 的.

                  If you instead set an event via an event handler property on the DOM element (e.g. button.onkeydown = function(evt) {...}) or using addEventListener/attachEvent (e.g. button.addEventListener("keydown", function(evt) {...}, false)) then just returning false from that function does not work in every browser and you need to do the returnValue and preventDefault() stuff from my other answer. preventDefault is specified in the DOM 2 spec and is implemented by most mainstream modern browsers. returnValue is IE-specific.

                  这篇关于为什么在 keydown 回调中返回 false 不会停止按钮单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:检查一个键是否已关闭? 下一篇:如何避免 JavaScript 中自动重复的 keydown 事件?

                  相关文章

                  最新文章

                    <bdo id='4pX9u'></bdo><ul id='4pX9u'></ul>

                  <small id='4pX9u'></small><noframes id='4pX9u'>

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