在开始这个问题之前,我不得不说我确实到处寻找答案,但一无所获。还尝试了很多东西,比如dispatchevent,postmessage,...但没有任何效果。
情况如下。
我有一个主窗口,其中有 4 个简单的按钮,如向下箭头、向上箭头、向左箭头和向右箭头。我想创建一个传递到主窗口中 iframe 的事件模拟。
在该 iframe 中加载了一个页面,其中有一个事件处理程序并对箭头做出反应。
我尝试遵循但没有成功
var event = document.createEvent('KeyboardEvent'); // create a key event define the event
event.initKeyboardEvent("keypress", // typeArg,
true, // canBubbleArg,
true, // cancelableArg,
null, // viewArg, Specifies UIEvent.view. This value may be null.
false, // ctrlKeyArg,
false, // altKeyArg,
false, // shiftKeyArg,
false, // metaKeyArg,
39, // keyCodeArg (39 is the right arrow key ),
0); // charCodeArg);
document.getElementById('vid').dispatchEvent(event);
有人知道我该如何解决这个问题吗?