問題描述
我想使用 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 控制臺模擬按鍵事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!