javascript document.createEvent 应该如何处理关键事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3941583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How is document.createEvent supposed to work with key events?
提问by Ernelli
I am trying to simulate keypresses in a web application, it is for an embedded system but it uses a Webkit derived browser. I have tested the code in Chrome and get the same error.
我正在尝试在 Web 应用程序中模拟按键,它适用于嵌入式系统,但它使用 Webkit 派生的浏览器。我已经在 Chrome 中测试了代码并得到相同的错误。
I tried to use code snippets from this example from Yahoo, but I keep getting the same error when firing the event using dispatchEvent. "target" is an HTML element in the DOM tree.
我尝试使用来自Yahoo 的此示例中的代码片段,但在使用dispatchEvent. “目标”是 DOM 树中的一个 HTML 元素。
function fireEvent(target) {
var evt = document.createEvent("UIEvent");
evt.initEvent("keypress", true, true);
target.dispatchEvent(evt);
}
It always throws:
它总是抛出:
"Error: UNSPECIFIED_EVENT_TYPE_ERR: DOM Events Exception 0"
“错误:UNSPECIFIED_EVENT_TYPE_ERR:DOM 事件异常 0”
I have tried createEvent("Events")as well and it always boils down to the same exception, both on the embedded system and in Chrome.
我也尝试createEvent("Events")过,它总是归结为相同的异常,无论是在嵌入式系统还是在 Chrome 中。
采纳答案by Ernelli
Ok, when doing further testing, it seemed that when all key event parameters was properly initialised, then dispatchEvent worked without fireing an exception.
好的,在进行进一步测试时,似乎当所有关键事件参数都正确初始化时,dispatchEvent 可以正常工作而不会触发异常。
The following code works.
以下代码有效。
function fireEvent(target) {
var evt = document.createEvent("Events");
evt.initEvent("keypress", true, true);
evt.view = window;
evt.altKey = false;
evt.ctrlKey = false;
evt.shiftKey = false;
evt.metaKey = false;
evt.keyCode = 0;
evt.charCode = 'a';
target.dispatchEvent(evt);
}
回答by Nicole Hu
Keypress is an UIEvent. You should use initUIEvent( 'type', bubbles, cancelable, windowObject, detail )rather than initEvent(). But for firefox, which implements a keyEvents, you should create a KeyEventsand initKeyEvents().
Keypress 是一个UIEvent. 您应该使用initUIEvent( 'type', bubbles, cancelable, windowObject, detail )而不是initEvent(). 但是对于实现 a 的 firefox,keyEvents您应该创建一个KeyEventsand initKeyEvents()。
回答by Shad
This one is old thread, just to update it I am adding another answer so that it makes more sense to any one.
这是一个旧线程,只是为了更新它,我添加了另一个答案,以便对任何人都更有意义。
initEvent() is deprecatedIt is still supported in some browsers but avoid using it.
initEvent() 已弃用某些浏览器仍支持它,但请避免使用它。
There is better concise way to create events like this
有更好的简洁方法来创建这样的事件
function fireEvent(target) {
var event = new Event('build');
// Listen for the event.
target.addEventListener('build', function (e) { ... }, false);
// Dispatch the event.
target.dispatchEvent(event);
}
To add more data to the event object, the CustomEvent interface exists and the detail property can be used to pass custom data. For example, the event could be created as follows:
要向事件对象添加更多数据,CustomEvent 接口存在并且可以使用 detail 属性来传递自定义数据。例如,可以按如下方式创建事件:
var event = new CustomEvent('build', { 'detail': target.dataset.time });
Reference: Creating and Triggering Events
参考:创建和触发事件

