从Javascript控制台模拟按键事件 [英] Simulating a Keypress Event from Javascript Console
问题描述
我想使用Javascript控制台模拟一个按键事件。我看到很多使用输入元素的答案。我不想使用输入元素。我只是想粘贴一些代码到Javascript控制台,并模拟一个按键事件(特别是后空间按钮)。
欢迎使用jQuery的答案。
a href =http://api.jquery.com/keypress/#keypress> .keypress
方法不接受模拟按键的参数。 p>
$(#target)。
将触发 #target
/ p>
如果您还想选择按下哪个键,可以使用 .trigger
。此示例来自文档:
var e = $ .Event(keydown,{keyCode:8}); //keydown如果这是你正在做的事
$(body)。trigger(e);
键码8是 让我知道如何为您效劳:
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).
Answers using jQuery are welcome.
jQuery has a .keypress
method accepting no arguments that simulates a keypress.
$("#target").keypress();
Will trigger a keypress on #target
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);
The key code 8 is the key code for backspace in JavaScript.
Let me know how that works for you :)
这篇关于从Javascript控制台模拟按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!