How jQuery ui disable default events and define custom events?
(like disable tab click event and define custom tabselect event)
- Where can I read about that deeply?
- Can you give places in jQuery ui code that it happens
- Can you give simple example to explain how its happens
Your question is a little vague, but I think you’re asking how jQuery implements
preventDefaultand sending custom events. That’s what I’ll answer.Preventing the Default Action and Propagation
I think what you mean when you talk about disabling default events is preventing a standard event’s default action. There’s no way to prevent a standard event from firing at all. However, for most events listeners can prevent the browser from taking the default action for that event. Default actions are things like following links or opening context menus. It’s also possible for a handler to stop the event from propagating further, which will prevent listeners registered on ancestors of the element it was registered on from being called. As with most things JavaScript, there are two ways to do things: the W3C standard way and Microsoft’s way.
The standard way of handling events in JavaScript is defined in W3C’s DOM Level 2 Events and, later, DOM Level 3 Events specifications. You attach an event listener to an element with its
addEventListenermethod. When the event is fired your listener function will be called and passed an event object. You can prevent the default action by calling the event’spreventDefaultmethod and stop it from propagating by calling itsstopPropagationmethod.Versions of Internet Explorer before 9 don’t support the W3C event system. You attach an event listener to an element with its
attachEventmethod. When the event is fired your listener function can access the event through thewindow.eventobject. You can prevent the default action by assigningfalseto itsreturnValueproperty and stop propagation by assigningtrueto itscancelBubbleproperty.For example, you might create an event listener that looks like this:
You could then register it on an element like this:
jQuery handles this in its
event.jsfile. Registering event listeners is handled by the privateaddfunction, which is called byjQuery.bindviajQuery.on. Stopping propagation and preventing the default are handled by the corresponding methods of thejQuery.Eventclass.Creating Custom Events
There are two ways to create custom events in JavaScript. It’s possible to ask the browser to fire a custom event on an element in both the W3C and Microsoft systems. That works, but it’s usually overkill. It’s much simpler to create an object that keeps track of a list of listener functions and provides methods for adding and removing listeners and dispatching events. Here’s a simple one:
That’s more-or-less what jQuery does, although of course theirs is far more complicated.