开发者

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

开发者 https://www.devze.com 2023-03-02 07:40 出处:网络
I was playing around with JavaScript a little while ago, and it irritated me that I couldn\'t create my own event.

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

精彩评论

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