dispatchEvent不会执行事件,但返回true [英] dispatchEvent not preforming event, but returns true

查看:708
本文介绍了dispatchEvent不会执行事件,但返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MDC的网站上,他们有一个 dispatchEvent的酷炫演示我的chrome 15。

On MDC's site they have a cool demo of dispatchEvent that works fine in my chrome 15.

我正在尝试获取一个事件对象并将其传递给dispatchEvent,并设置了这里的一个简单案例,你将事件记录为一个数组,然后重播它们。

I'm trying to take an event object and pass it into dispatchEvent, and have set up a simple case here where you record events as an array then will replay them.

本质上,我设置了单击一个窗口监听器,然后执行 window.dispatchEvent(recordedEvent)

In essence, I set up a window listener for click, and then preform window.dispatchEvent(recordedEvent).

我无法确定我的事件监听器中的事件对象与MDC示例中的 initMouseEvent 中的事件的预处理方式不同。

I'm unable to determine why my event object from the event listener will not preform the same way as the event from initMouseEvent in the MDC example.

我真的不担心让它工作,我想知道为什么这不起作用,当阅读搞笑手册它似乎意味着它应该。

I'm not really worried with making it work, I want to know why this doesn't work, when after reading the funny manual it seems to imply it should.

推荐答案

看起来它对我来说效果很好; 此处是更新。

It seems like it works just fine to me; here is an update.

编辑—等待 - 你是否担心它会返回 true ,就像调用了preventDefault()一样?如果是这样,那么我现在理解你的困惑。

edit — wait - is your concern about the fact that it's returning true as if "preventDefault()" was called? If so then I understand your confusion now.

最后编辑好的我想我看到了这个问题。当您调度该事件时,您始终从窗口发送。如果您跟踪所涉及的元素,那么它就有效。

edit finally OK I think I see the issue. When you're dispatching the event, you always dispatch from window. If instead you keep track of the elements involved, then it works.

以下是适用的好代码(适用于Firefox 7):

Here's the good code that works (for me in Firefox 7):

//Vars for the elements we're working with
var replay = document.getElementById("replay");
var replaying = false;

//Handler to record events into a data array.
var handler = function (e) {
    if (replaying) {
        console.log("replay " + e.type);
    }
    else if (e.target.tagName.toLowerCase() !== 'input') {
        return;
            }
    else {
        handler.data.push({elem: e.target, event: e});
        console.log(handler.data);
    }
};
handler.data = [];

//Listen for the click on the replay button   
replay.addEventListener("click", function(){
    //Remove listeners so we don't create some crazy
    //infinite paradox with turtles all the way down
    // window.removeEventListener("click", handler);
    replaying = true;

    //Clear the textbox out
    var status = [], obj;
    //Dispatch a bunch of stored up events
    for (var i=0; i<handler.data.length;i++){
        obj = handler.data[i];
        status.push(obj.elem.dispatchEvent(obj.event));
    }
    console.log(status);
    replaying = false;
});

//Listen for some specific events
//window.addEventListener("keyup", handler);
window.addEventListener("click", handler);

另请注意,最好避免在重播按钮本身上保存点击事件。

Also note that it's good to avoid saving the "click" events on the "Replay" button itself.

这篇关于dispatchEvent不会执行事件,但返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆