为什么在某些浏览器中,输入字段的onkeypress处理器不接收箭头键、选项卡等?

时间:2022-06-06 19:33:30

I'm building a text input field specialized for entering and editing times. One of the parts of the functionality calls for various ways to focus on the different components of the time (hours, minutes, seconds), which I indicate through a text selection. Direct selection is possible with the mouse and this is working great. The other feature is keyboard navigation.

我正在构建一个用于输入和编辑时间的文本输入字段。该功能的其中一个部分需要不同的方法来关注时间的不同组件(小时、分钟、秒),我通过文本选择来表示。直接选择是可能的鼠标,这是伟大的工作。另一个功能是键盘导航。

Most of this functionality relies on the fact that I'm able to handle keyPress events, suppress the default behavior and substitute a special action instead.

这个功能的大部分依赖于这样一个事实:我能够处理按键事件,抑制默认行为并替换一个特殊动作。

In Firefox, I have this working nicely. The user may use left/right arrow keys or tab/shift-tab to move between parts of the time (and when they get to the end, the next tab key will leave the field and focus the next element normally).

在Firefox中,我让它运行得很好。用户可以使用左/右箭头键或tab/shift-tab在不同的时间点之间移动(当它们到达末尾时,下一个tab键将离开字段,并正常地聚焦下一个元素)。

In Internet Explorer 7 (potentially others?) the arrow keys and tab are not even received by the keypress handler. If arrow keys are pressed, the text selection is lost and the cursor moves by one. The effect of providing multiple fields disappears and it results in the control feeling broken. Tab also seems to skip the handler and just immediately flips to the next focusable element.

在Internet Explorer 7(可能是其他的吗?)中,键和选项卡甚至没有被按键处理程序接收到。如果按下箭头键,文本选择就会丢失,光标会移动一个。提供多个字段的效果消失了,导致控制感觉被破坏。Tab还可以跳过处理程序,直接切换到下一个可聚焦元素。

Are there any tricks to intercepting these keys?

有什么诀窍可以截获这些密钥吗?

1 个解决方案

#1


7  

You need to use onkeydown for non-character keys. onkeypress in IE only handles keys that return a string.

您需要对非字符键使用onkeydown。onkeypress在IE中只处理返回字符串的键。

To specifically quote the MSDN documentation:

特别引用MSDN文件:

As of Microsoft Internet Explorer 4.0, the onkeypress event fires and can be canceled for the following keys:

从microsoftinternetexplorer4.0开始,onkeypress事件会触发,并可取消下列按键:

  • Letters: A - Z (uppercase and lowercase)
  • 字母:A - Z(大写和小写)
  • Numerals: 0 - 9
  • 数字:0 - 9
  • Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
  • 符号:!@ # $ % ^ & *()_ - + = <[]{ },。/ ?\ | ' ' ~。
  • System: ESC, SPACEBAR, ENTER
  • 系统:ESC,空格键,输入

#1


7  

You need to use onkeydown for non-character keys. onkeypress in IE only handles keys that return a string.

您需要对非字符键使用onkeydown。onkeypress在IE中只处理返回字符串的键。

To specifically quote the MSDN documentation:

特别引用MSDN文件:

As of Microsoft Internet Explorer 4.0, the onkeypress event fires and can be canceled for the following keys:

从microsoftinternetexplorer4.0开始,onkeypress事件会触发,并可取消下列按键:

  • Letters: A - Z (uppercase and lowercase)
  • 字母:A - Z(大写和小写)
  • Numerals: 0 - 9
  • 数字:0 - 9
  • Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
  • 符号:!@ # $ % ^ & *()_ - + = <[]{ },。/ ?\ | ' ' ~。
  • System: ESC, SPACEBAR, ENTER
  • 系统:ESC,空格键,输入