javascript, simulate keyboard events, work on chrome(webkit)

前端 未结 1 1181
甜味超标
甜味超标 2020-12-18 14:01

in FF, i\'ve used this code:


if (keyCount == lineLimit) {
    // method in FF, no Chrome
    var mock = document.createEvent(\"KeyboardEvent\"); // or KeysEvent         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-18 15:00

    This works, but it's not generating a keypress-event, rather a text-insert event.

    var te = document.createEvent('TextEvent');
    te.initTextEvent('textInput', true, true, window, 'test');
    .dispatchEvent(te);
    

    That inserts the word 'test' at the end of the input (in your case you'd probably replace that by \n.

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