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

    <tfoot id='OdBRQ'></tfoot>

          <bdo id='OdBRQ'></bdo><ul id='OdBRQ'></ul>
      1. <legend id='OdBRQ'><style id='OdBRQ'><dir id='OdBRQ'><q id='OdBRQ'></q></dir></style></legend>

        按向上箭头时防止文本输入中的默认行为

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

                <tbody id='nHD3r'></tbody>
              • <small id='nHD3r'></small><noframes id='nHD3r'>

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

                  <legend id='nHD3r'><style id='nHD3r'><dir id='nHD3r'><q id='nHD3r'></q></dir></style></legend>
                  本文介绍了按向上箭头时防止文本输入中的默认行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用带有数值的基本 HTML <input type="text"/> 文本字段.

                  I’m working with basic HTML <input type="text"/> text field with a numeric value.

                  我正在添加 JavaScript 事件 keyup 以查看用户何时按下向上箭头键 (e.which == 38) - 然后我增加数值.

                  I’m adding JavaScript event keyup to see when user presses arrow up key (e.which == 38) – then I increment the numeric value.

                  代码运行良好,但有一件事让我很头疼.当我按向上箭头键时,Safari/Mac 和 Firefox/Mac 都会在最开始移动光标.据我所知,这是每个 <input type="text"/> 文本字段的默认行为,这是有道理的.

                  The code works well, but there’s one thing that bugs me. Both Safari/Mac and Firefox/Mac move cursor at the very beginning when I’m pressing the arrow up key. This is a default behavior for every <input type="text"/> text field as far as I know and it makes sense.

                  但这不会产生光标前后跳动的非常美观的效果(在更改值之后).

                  But this creates not a very aesthetic effect of cursor jumping back and forward (after value was altered).

                  一开始的跳转发生在 keydown 上,但即使有了这些知识,我也无法阻止它发生.我尝试了以下方法:

                  The jump at the beginning happens on keydown but even with this knowledge I’m not able to prevent it from occuring. I tried the following:

                  input.addEventListener('keydown', function(e) {
                      e.preventDefault();
                  }, false);
                  

                  e.preventDefault() 放入 keyup 事件也无济于事.

                  Putting e.preventDefault() in keyup event doesn’t help either.

                  有什么办法可以防止光标移动吗?

                  Is there any way to prevent cursor from moving?

                  推荐答案

                  要保留光标位置,请在更改值之前备份 input.selectionStart.

                  To preserve cursor position, backup input.selectionStart before changing value.

                  问题在于 WebKit 对 keydown 做出反应,而 Opera 更喜欢 keypress,因此存在问题:两者都被处理和限制.

                  The problem is that WebKit reacts to keydown and Opera prefers keypress, so there's kludge: both are handled and throttled.

                  var ignoreKey = false;
                  var handler = function(e)
                  {
                      if (ignoreKey)
                      {
                          e.preventDefault();
                          return;
                      }
                      if (e.keyCode == 38 || e.keyCode == 40) 
                      {
                          var pos = this.selectionStart;
                          this.value = (e.keyCode == 38?1:-1)+parseInt(this.value,10);        
                          this.selectionStart = pos; this.selectionEnd = pos;
                  
                          ignoreKey = true; setTimeout(function(){ignoreKey=false},1);
                          e.preventDefault();
                      }
                  };
                  
                  input.addEventListener('keydown',handler,false);
                  input.addEventListener('keypress',handler,false);
                  

                  这篇关于按向上箭头时防止文本输入中的默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 javascript 检测硬件键盘的存在? 下一篇:jQuery .keypress &amp;.keydown .which

                  相关文章

                  最新文章

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

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

                  <tfoot id='nlFaL'></tfoot>

                        <bdo id='nlFaL'></bdo><ul id='nlFaL'></ul>