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

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

      <legend id='m9o2j'><style id='m9o2j'><dir id='m9o2j'><q id='m9o2j'></q></dir></style></legend>
      • <bdo id='m9o2j'></bdo><ul id='m9o2j'></ul>

    1. event.keycode 与 event.which

      时间:2023-09-04
      <tfoot id='5Hnaw'></tfoot>

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

      <small id='5Hnaw'></small><noframes id='5Hnaw'>

        • <bdo id='5Hnaw'></bdo><ul id='5Hnaw'></ul>

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

                <legend id='5Hnaw'><style id='5Hnaw'><dir id='5Hnaw'><q id='5Hnaw'></q></dir></style></legend>
                本文介绍了event.keycode 与 event.which的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我犯了 Firefox keydown 的行为,因为在没有关注特定字段的情况下按下回车键(实际上是任何键)不会触发 keydown 事件只会触发 keypress 事件.

                I fell foul of a Firefox keydown behavior in that pressing the enter key (indeed any key) without having focus on a specific field will NOT trigger a keydown event it will only trigger a keypress event.

                这可能会非常令人困惑,因为 keydownkeyup 事件使用 JavaScript 键码,而 keypress 使用 ASCII 码.幸运的是 13(输入/返回)对两者都是通用的.

                This could be very confusing as the keydown and keyup event use JavaScript key codes whereas keypress uses ASCII codes. Fortunately 13 (enter/return) is common to both.

                FF 在这种情况下使用 keypress 有什么已知的原因吗?有什么好处?

                Is there any known reason why FF using keypress in this circumstance? What is the benefit?

                一旦确定了这一点,IE8 就抛出了一个愚蠢的问题,因为它不允许 preventDefault 要求而不是 returnValue = false 来自另一个 SO 帖子的以下片段被证明非常有用:

                Once this was established IE8 threw up a silly in that it does not permit preventDefault demanding instead returnValue = false the following snippet from another SO post has proved very useful:

                event.preventDefault ? event.preventDefault() : event.returnValue = false;
                

                在寻找解决这些问题的过程中,我一直被 event.keycodeevent.which 混淆.也就是说,我使用类似于以下的 switch 语句做错了:

                During the search to resolve these issues I have been consistently confused by event.keycode vs event.which. Namely am I doing wrong using a switch statement similar to:

                $("#class_Name").bind("keydown", function(event){
                    // do not test input if field controls used
                    switch(event.which){
                       case 13:
                       //enter key 
                       event.preventDefault ? event.preventDefault() : event.returnValue = false;
                       break;
                     }
                


                以下是否更好,如果是,为什么?


                Is the following better, if so why?

                $("body").keypress(function(event){
                     // stop inadvertant form submission
                     if (event.keycode == "13"){
                       event.preventDefault ? event.preventDefault() : event.returnValue = false;
                     }
                });
                


                我只是想知道,以便我知道哪个最适合申请.


                I would just like to know so that I know which is best to apply.

                非常感谢.

                推荐答案

                有些浏览器使用keyCode,有些使用which.但是对于 jQuery,这是 规范化的,所以你不必考虑这一点.你可以选择你喜欢的那个.

                Some browsers use keyCode and others use which. But with jQuery this is normalized so you don't have to think about that. You can just choose the one you prefer.

                这篇关于event.keycode 与 event.which的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么必须将事件对象作为参数传递? 下一篇:在内部元素上触发点击事件

                相关文章

                最新文章

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

                    <bdo id='khqVb'></bdo><ul id='khqVb'></ul>
                  <tfoot id='khqVb'></tfoot>

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