dispatchEvent 不执行事件,但返回 true

发布于 2024-12-15 06:19:12 字数 679 浏览 4 评论 0原文

在 MDC 的网站上,他们有一个 dispatchEvent 的酷演示,在我的 Chrome 中运行良好15.

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

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

我无法确定为什么事件侦听器中的事件对象的执行方式与 MDC 示例中的 initMouseEvent 中的事件不同。

我并不是真的担心让它发挥作用,我想知道为什么这不起作用,当阅读有趣的手册似乎暗示它应该这样做。

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

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2024-12-22 06:19:12

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

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

最后编辑 好的,我想我明白了这个问题。当您分派事件时,您始终从窗口分派。相反,如果您跟踪所涉及的元素,那么它就有效。

下面是有效的代码(对我来说在 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);

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文