dispatchEvent 不执行事件,但返回 true
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来它对我来说效果很好; 此处是更新。
编辑 - 等等 - 您是否担心它返回
true
就像调用“preventDefault()”一样?如果是这样,那么我现在理解您的困惑。最后编辑 好的,我想我明白了这个问题。当您分派事件时,您始终从
窗口
分派。相反,如果您跟踪所涉及的元素,那么它就有效。下面是有效的代码(对我来说在 Firefox 7 中):
另请注意,最好避免在“重播”按钮本身上保存“单击”事件。
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):
Also note that it's good to avoid saving the "click" events on the "Replay" button itself.