automatically press keyboard with jQuery?

前端 未结 6 1918
臣服心动
臣服心动 2021-01-19 04:06

Is it possible with jQuery to automatically simulate a press on a keyboard, f.ex. inside an html input field?

As explanation: If I press the a inside an

6条回答
  •  执笔经年
    2021-01-19 04:42

    It seems to me as if the goal is not necessarily to mimic a native browser keypress event, but to manipulate text inside an input field.

    If I'm right, you can fetch the .prop('selectionStart') and .prop('selectionEnd') values and overwrite any characters within those indexes by the character(s) you want to "keypress". You can accompany this by triggering of appropriate keyboard events.

    I've never worked on text selection before. I know IE handles things differently than normal browsers, so you'll need to look into this topic and find a cross-browser solution if you choose to walk this path. To make things perfect, you'll need to change the selection properties after modifying the input's value as if it were a native paste.

提交回复
热议问题