问题描述
我正在尝试模拟JavaScript中的实际Tab键按下。我不想专注于下一个元素或类似的东西,我只是想让它看起来像是按Tab键。
I'm trying to simulate an actual tab key press in JavaScript. I don't want to focus on the next element or anything like that, I just want to make it seem like the tab key has been pressed.
原因是因为我正在构建一个表单JavaScript类,我希望能够像tab一样使用enter键。如果有人使用原生BROWSER自动完成功能,我需要触发Tab键以捕获选定的自动完成响应。如果我只是移动到下一个输入,它将不会捕获他们的自动完成选择并将该字段留空。
The reason why is because I am building a form JavaScript class where I want to be able to use the enter key just like tab. If someone is using a native BROWSER autocomplete, I need to fire the tab key to capture the selected autocomplete response. If I just move to the next input it won't capture their autocomplete selection and leave the field blank.
有任何想法吗?
推荐答案
我不认为这是可能的; ...提到发布事件不会出于安全原因,触发用户操作的默认结果;该脚本不应该能够直接模拟用户交互。您必须模拟按键引起的行为(例如专注于某个字段),而不是尝试实际模拟按键。您可能无法与浏览器的本机自动完成功能进行交互,除非浏览器明确提供了这样做的方法。
I don't think it's possible; an article about DOM events here ...mentions that firing an event doesn't trigger the default result of the user action, for security reasons; the script should not be able to simulate user interaction directly. You will have to simulate the behavior the keypress causes (such as focus on a field), instead of trying to actually simulate a keypress. You probably won't be able to interact with the browser's native autocomplete functionality, unless the browser explicitly provides a means for you to do so.
编辑:
另请参阅:[(自动填充HTML属性)
See also: [https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-autocomplete] (Autocomplete HTML attribute)
这篇关于JavaScript中的Fire tab按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!