开发者

Is there a built in way to make custom events in JavaScript natively?

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

I've seen Framework's with this built in (jQuery, MooTools, Prototype. Dojo doesn't which is weird because it seem开发者_运维技巧s 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);

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

I use this technique to create custom drag and resize events for communication on this sample page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜