Simulating a Keypress Event from Javascript Console

前端 未结 1 1848
醉话见心
醉话见心 2020-12-06 03:10

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 s

相关标签:
1条回答
  • 2020-12-06 03:19

    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 :)

    0 讨论(0)
提交回复
热议问题