我想从控制台使用纯JavaScript来触发按键事件,但无法这样做。下面纯JavaScript触发按键事件
是我的代码
var abc = document.querySelector("#main > footer > div.block-compose > div.input-container > div > div.input");
代码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");
代码2
var event = document.createEvent("KeyboardEvent");
event.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 115, 0);
任何建议??
Jsfiddel链接:
http://jsfiddle.net/mishragaurav31/VLZGk/1105/
我只是希望能够警示 “的keyPressed”
你是否看到任何错误? – Rajesh
嘿,如果可能的话创建jsfiddle或者添加你的html代码也 –
看看这个答案:http://stackoverflow.com/questions/22574431/testing-keydown-events-in-jasmine-with-specific-keycode/23700583 #23700583 – MarcoL