问题描述
我想使用 Javascript 控制台模拟 Keypress 事件.我看到很多使用输入元素的答案.我不想使用输入元素.我只想将一些代码粘贴到 Javascript 控制台中并模拟一个按键事件(特别是退格按钮).
I want to simulate a Keypress event using the Javascript Console. I see lots of answers using an input element. I don't want to use an input element. I just want to paste some code into the Javascript console and have a keypress event simulated(specifically the back space button).
欢迎使用 jQuery 回答.
Answers using jQuery are welcome.
推荐答案
jQuery 有一个 .keypress
方法不接受模拟按键的参数.
jQuery has a .keypress
method accepting no arguments that simulates a keypress.
$("#target").keypress();
将触发 #target
如果您还想选择按下哪个键,您可以使用 .trigger
.这个例子来自 docs:
If you'd like to also select which key was pressed, you can use .trigger
. This example is from the docs:
var e = $.Event("keydown", { keyCode: 8}); //"keydown" if that's what you're doing
$("body").trigger(e);
键码 8 是 JavaScript 中的 退格键码.
The key code 8 is the key code for backspace in JavaScript.
让我知道这对你有什么作用:)
Let me know how that works for you :)
这篇关于从 Javascript 控制台模拟按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!