You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/events/event-extensions.md
+43-42Lines changed: 43 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,7 @@ chapter : events
3
3
section : 7
4
4
title : jQuery Event Extensions
5
5
attribution: Dave Methvin
6
+
github: dmethvin
6
7
---
7
8
jQuery offers several ways to extend its event system to provide custom functionality when events are attached to elements. Internally in jQuery, these extensions are primarily used to ensure that standard events such as `submit` and `change` behave consistently across browsers. However, they can also be used to define new events with custom behavior.
8
9
@@ -43,10 +44,10 @@ To add a property name to this list, use `jQuery.event.props.push("newPropertyNa
43
44
The fixHooks interface provides a per-event-type way to extend or normalize the event object that jQuery creates when it processes a *native* browser event. A fixHooks entry is an object that has two properties, each being optional:
44
45
45
46
props: Array
46
-
:Strings representing properties that should be copied from the browser's event object to the jQuery event object. If omitted, no additional properties are copied beyond the standard ones that jQuery copies and normalizes (e.g., event.target and event.relatedTarget).
47
+
:Strings representing properties that should be copied from the browser's event object to the jQuery event object. If omitted, no additional properties are copied beyond the standard ones that jQuery copies and normalizes (e.g., event.target and event.relatedTarget).
47
48
48
49
filter: Function( event, originalEvent )
49
-
:jQuery calls this function after it constructs the jQuery.Event object, copies standard properties from `jQuery.event.props`, and copies the fixHooks-specific props (if any) specified above. The function can create new properties on the event object or modify existing ones. The second argument is the browser's native event object, which is also availble in `event.originalEvent`.
50
+
:jQuery calls this function after it constructs the jQuery.Event object, copies standard properties from `jQuery.event.props`, and copies the fixHooks-specific props (if any) specified above. The function can create new properties on the event object or modify existing ones. The second argument is the browser's native event object, which is also availble in `event.originalEvent`.
50
51
51
52
Note that for all events, the browser's native event object is available in `event.originalEvent`; if the jQuery event handler examines the properties there instead of jQuery's normalized `event` object, there is no need to create a fixHooks entry to copy or modify the properties.
52
53
@@ -58,7 +59,7 @@ For example, to set a hook for the "drop" event that copies the "dataTransfer" p
58
59
Since fixHooks are an advanced feature and rarely used externally, we have not added extra code and interfaces to deal with conflict resolution. If there is a chance that some other code may be assigning fixHooks to the same events, the code should check for an existing hook and take appropriate measures. A simple solution might look like this:
59
60
60
61
<javascript>if ( jQuery.event.fixHooks.drop ) {
61
-
throw new Error("Someone else took the jQuery.event.fixHooks.drop hook!");
62
+
throw new Error("Someone else took the jQuery.event.fixHooks.drop hook!");
@@ -93,30 +94,30 @@ When defined, these string properties specify that a special event should be han
93
94
The behavior of these properties is easiest to see with an example. Assume a special event defined as follows:
94
95
95
96
<javascript>jQuery.event.special.pushy = {
96
-
bindType: "click",
97
-
delegateType: "click"
97
+
bindType: "click",
98
+
delegateType: "click"
98
99
};
99
100
</javascript>
100
101
101
102
When these properties are defined, the following behavior occurs in the jQuery event system:
102
103
103
-
*Event handlers for the "pushy" event are actually attached to "click" -- both directly bound and delegated events.
104
-
*Special event hooks for "click" are called if they exist, *except* the `handle` hook for "pushy" is called when an event is delivered if one exists.
105
-
*Event handlers for "pushy" must be removed using the "pushy" event name, and are unaffected if "click" events are removed from the same elements.
104
+
*Event handlers for the "pushy" event are actually attached to "click" -- both directly bound and delegated events.
105
+
*Special event hooks for "click" are called if they exist, *except* the `handle` hook for "pushy" is called when an event is delivered if one exists.
106
+
*Event handlers for "pushy" must be removed using the "pushy" event name, and are unaffected if "click" events are removed from the same elements.
106
107
107
108
108
109
So given the special event above, this code shows that a pushy isn't removed by removing clicks. That might be an effective way to defend against an ill-behaved plugin that didn't namespace its removal of click events, for example:
109
110
110
111
<javascript>var $p = $("p");
111
112
$p.on("click", function( e ) {
112
-
$("body").append("I am a " + e.type + "!"));
113
+
$("body").append("I am a " + e.type + "!"));
113
114
});
114
115
$p.on("pushy", function( e ) {
115
-
$("body").append("I am pushy but still a " + e.type + "!");
116
+
$("body").append("I am pushy but still a " + e.type + "!");
116
117
});
117
-
$p.trigger("click");// triggers both handlers
118
+
$p.trigger("click");// triggers both handlers
118
119
$p.off("click");
119
-
$p.trigger("click");// still triggers "pushy"
120
+
$p.trigger("click");// still triggers "pushy"
120
121
$p.off("pushy");
121
122
</javascript>
122
123
@@ -130,22 +131,22 @@ Many of the special event hook functions below are passed a `handleObj` object t
130
131
</javascript>
131
132
132
133
type: String
133
-
:The type of event, such as `"click"`. When special event mapping is used via bindType or delegateType, this will be the mapped type.
134
+
:The type of event, such as `"click"`. When special event mapping is used via bindType or delegateType, this will be the mapped type.
134
135
135
136
origType: String
136
-
:The original type name (in this case, `"click"`) regardless of whether it was mapped via bindType or delegateType. So when a "pushy" event is mapped to "click" its origType would be "pushy". See the examples in those special event properties above for more detail.
137
+
:The original type name (in this case, `"click"`) regardless of whether it was mapped via bindType or delegateType. So when a "pushy" event is mapped to "click" its origType would be "pushy". See the examples in those special event properties above for more detail.
137
138
138
139
namespace: String
139
-
:Namespace(s), if any, provided when the event was attached, such as `"myPlugin"`. When multiple namespaces are given, they are separated by periods and sorted in ascending alphabetical order. If no namespaces are provided, this property is an empty string.
140
+
:Namespace(s), if any, provided when the event was attached, such as `"myPlugin"`. When multiple namespaces are given, they are separated by periods and sorted in ascending alphabetical order. If no namespaces are provided, this property is an empty string.
140
141
141
142
selector: String
142
-
:For delegated events, this is the selector used to filter descendant elements and determine if the handler should be called. In the example it is `"button"`. For directly bound events, this property is `null`.
143
+
:For delegated events, this is the selector used to filter descendant elements and determine if the handler should be called. In the example it is `"button"`. For directly bound events, this property is `null`.
143
144
144
145
data: Object
145
-
:The data, if any, passed to jQuery during event binding, e.g., `{myData: 42}`. If the data argument was omitted or `undefined`, this property is `undefined` as well.
146
+
:The data, if any, passed to jQuery during event binding, e.g., `{myData: 42}`. If the data argument was omitted or `undefined`, this property is `undefined` as well.
146
147
147
148
handler: function( event: jQuery.Event )
148
-
:Event handler function passed to jQuery during event binding; in the example it is a reference to `myHandler`. If `false` was passed during event binding, the handler refers to a single shared function that simply returns `false`.
149
+
:Event handler function passed to jQuery during event binding; in the example it is a reference to `myHandler`. If `false` was passed during event binding, the handler refers to a single shared function that simply returns `false`.
149
150
150
151
#### setup: function( data: Object, namespaces, eventHandle: function )
151
152
@@ -191,26 +192,26 @@ This `multiclick` special event maps itself into a standard click event, but use
191
192
The hook stores the current click count in the data object, so multiclick handlers on different elements don't interfere with each other. It changes the event type to the original "multiclick" type before calling the handler and restores it to the mapped "click" type before returning:
192
193
193
194
<javascript>jQuery.event.special.multiclick = {
194
-
delegateType: "click",
195
-
bindType: "click",
196
-
handle: function( event ) {
197
-
var handleObj = event.handleObj,
198
-
targetData = jQuery.data( event.target ),
199
-
ret;
200
-
201
-
// If a multiple of the click count, run the handler
0 commit comments