Pitfalls of Simulating Keyboard Input with JS
Pitfalls At first I searched around on Google and found a jQuery solution. The amount of code was small, it looked great and promising, but it didn't work at all…… I tried these versions over and over: var e = $.Event('keyup') e.keyCode = 49 $('#btn').trigger(e) // just replace keycode with which $('#btn').trigger($.Event('keyup', { which: 4
目录4 个章节
Pitfalls
At first I searched around on Google and found a jQuery solution. The amount of code was small, it looked great and promising, but it didn't work at all……
I tried these versions over and over:
var e = $.Event('keyup')
e.keyCode = 49
$('#btn').trigger(e)
// 就是把 keycode 换成 which
$('#btn').trigger($.Event('keyup', { which: 49 }))
Didn't work at all……
Then I looked at the event listener in the developer debugging tools and found that the input box was listening for the input event. So I tried setting the input's value first, then using jQuery to simulate triggering the event to see if it would work~
$('#btn').trigger('input')
That didn't work either
Solution
Then I found a pretty good method: using native JS InputEvent. For older Chrome, use UIEvent instead; the effect is basically the same.
The code is as follows
Using InputEvent:
var dom = document.querySelector('#selector')
var evt = new InputEvent('input', {
inputType: 'insertText',
data: st,
dataTransfer: null,
isComposing: false
});
dom.value = '输入的内容';
dom.dispatchEvent(evt);
Using UIEvent:
var dom = document.querySelector('#selector')
var evt = new UIEvent('input', {
bubbles: false,
cancelable: false
});
dom.value = st;
dom.dispatchEvent(evt);
Personally tested, it works perfectly
You can wrap it into a function yourself for more convenient calling; I won't paste that part of the code~
References
- Useful reference: http://fengyitong.name/index.php/archives/38/
- Simulating keyboard input with js+JQuery: https://blog.csdn.net/qq_45741976/article/details/102645358
- A simple website for getting keycodes: https://keycode.info/
Welcome to Discuss
Programming Lab focuses on exploring popular new internet technologies and team agile development practices. Reply with linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account "Programming Lab" to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~

- Cnblogs: https://www.cnblogs.com/deali/
- Coding livestream: https://live.bilibili.com/11883038
- Zhihu: https://www.zhihu.com/people/dealiaxy
_EOF_
Pitfalls of Simulating Keyboard Input with JS
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
NPM-Warn--ENOENT--no-such-file-or-directory
Now type the command to auto generate the package.json npm init --yes
2022-07-18 · Web前端 · 约 1 分钟
使用mock.js开发假数据接口
参考资料 Mockjs使用 - https://juejin.cn/post/6953199521031520292 http://wll8.gitee.io/mockjs-examples Mokcjs 文档 Vitepress版 - http://mock.pe666.cn/
2022-12-06 · Web前端 · 约 1 分钟
开始开发一个新博客了,复古风Metro风格,顺便diss一下js
emmm 我原本的博客是用wordpress搭建的,当年七牛云还是免费时用它做的CDN,但是现在收费了,如果维持目前的博客流量的话每个月都要支付不少费用,于是只好把七牛云给停了= =,这也导致我博客里面很多图片都访问不了,页面布局也出问题了,不想折腾了,而且wordpress用起来总觉得不顺手,还是自己做一个吧~ 计划 最近刚好在学SpringBoot啥的,本来想用springboot搭建一个博客
2022-07-18 · Web前端 · 约 1 分钟
魔改editormd组件,优化ToC渲染效果
前言 我的StarBlog博客目前使用 editor.md 组件在前端渲染markdown文章,但这个组件自动生成的ToC(内容目录)不是很美观,我之前魔改过一个树形组件 BootStrap-TreeView,所以就想要用这个树形组件来展示ToC。 原本的效果是这样的 image-20221020112752560 我魔改完的效果 image-20221020112914518 先分析一波 首先看
2022-10-20 · Web前端 · 约 1 分钟
讨论
共 0 条加载评论中…