是否有内置的方式在JavaScript中本地生成自定义事件? [英] Is there a built in way to make custom events in JavaScript natively?

查看:131
本文介绍了是否有内置的方式在JavaScript中本地生成自定义事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才玩JavaScript时,我无法创建自己的事件让我感到恼火。

I was playing around with JavaScript a little while ago, and it irritated me that I couldn't create my own event.

我看过这个框架了内置(jQuery,MooTools,Prototype.Dojo并不奇怪,因为它似乎做了所有事情和你的洗衣店)而且我实际上构建了自己的系统来创建和解雇自定义事件。

I've seen Framework's with this built in (jQuery, MooTools, Prototype. Dojo doesn't which is weird because it seems to do everything and your laundry) and I actually built my own system for creating and firing custom events.

感觉应该有一种本地的方式来做到这一点。有没有人知道怎么做/如果你可以?

It just feels like there should be a native way to do it. Does anyone know how to do this/if you even can?

推荐答案

var dragEvent = document.createEvent("Event");
dragEvent.initEvent("dragged", true, true);
el.dispatchEvent(dragEvent);

有关官方规格,请参阅 DOM Level 2 Events 。另请参见 createEvent() initEvent() / initMouseEvent() / initUIEvent() dispatchEvent() at MDC。

For official specs, see DOM Level 2 Events. See also createEvent(), initEvent() / initMouseEvent() / initUIEvent(), and dispatchEvent() at MDC.

我使用此技术为此示例页面

这篇关于是否有内置的方式在JavaScript中本地生成自定义事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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