简体   繁体   English

Javascript .`keypress` 事件未在移动设备上触发

[英]Javascript .`keypress` event not firing on mobile device

On this page:在本页:

http://www.practicalnetworking.net/subnet.html http://www.practicalnetworking.net/subnet.html

If you click on any of the input boxes in the "IP Address" column, and press the "."如果单击“IP 地址”列中的任何输入框,然后按“.”。 or "/" keys (period or slash) it jumps you to the next input box.或“/”键(句点或斜线)它会跳转到下一个输入框。

Or at least, it does on a desktop browser.或者至少,它在桌面浏览器上是这样。 On a mobile browser, it doesn't seem to register the onkeypress event.在移动浏览器上,它似乎没有注册onkeypress事件。

This is the code that is enabling the "jump" on period or slash presses:这是在句点或斜线按下时启用“跳转”的代码:

        // Function to jump to next box on . or / keys
        function jumpdot(event) {
            // Capture pressed key:
            var y = event.key;

            if (y == "." || y == "/" ) {
                // . or / was pressed, jump to next userinput box and prevent typing of . or /
                event.preventDefault();
                document.getElementsByName(uiNext)[0].focus();
            }
        }

Is there an easy way to enable that functionality on Mobile phones as well?是否有一种简单的方法也可以在手机上启用该功能?

The keypress event is marked as Legacy in the DOM-Level-3 Standard . keypress事件在DOM-Level-3标准中标记为Legacy

Warning. 警告。 The keypress event type is defined in this specification for reference and completeness, but this specification deprecates the use of this event type. keypress事件类型在本规范中定义,以供参考和完整,但此规范不建议使用此事件类型。

Use the keydown event instead. 请改用keydown事件。 Info: Keydown Event in Mozilla Developer 信息: Mozilla开发人员的Keydown事件

You should also consider ... 你还应该考虑......

KeyboardEvent.which : Warning: This attribute is deprecated; KeyboardEvent.which:警告:不推荐使用此属性; you should use KeyboardEvent.key instead, if available. 你应该使用KeyboardEvent.key,如果可用的话。

KeyboardEvent.keyCode : Warning: This attribute is deprecated; KeyboardEvent.keyCode:警告:不推荐使用此属性; you should use KeyboardEvent.key instead, if available. 你应该使用KeyboardEvent.key,如果可用的话。

To read the pressed Key , use instead event.key 要读取按下的键,请使用event.key

ev.keyCode can be helpful. ev.keyCode可能会有所帮助。 That provides more info about a keystroke. 这提供了有关击键的更多信息。

使用oninput:而不是onkeypress:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM