简体   繁体   中英

Pure javascript to trigger keypress event

i am trying to trigger keypress event using pure javascript from console but unable to do so.

below is my code

var abc = document.querySelector("#main > footer > div.block-compose > div.input-container > div > div.input");

Code 1:

function fire( elem, type ) {

  var evt = elem.createEvent("Events");

  evt.initEvent( type, true, true, window, 1);

  elem.dispatchEvent(evt);

}

document.addEventListener( "plop", function() {
   console.log( "Fired a synthetic click event" );
}, false );

fire( abc, "plop" );

Code 2

var event = document.createEvent("KeyboardEvent");

event.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 115, 0);

Any suggestion??

Jsfiddel link:

http://jsfiddle.net/mishragaurav31/VLZGk/1105/

i just want javascript code that can alert "keypressed"

怎么样(通过onkeydown调用发送密钥): jsBin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM