-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.on.off.js
More file actions
43 lines (40 loc) · 1.41 KB
/
jquery.on.off.js
File metadata and controls
43 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(function($){
"use strict";
var undelegateOrUnbind = function(events, selector, handler){
return ($.type(selector) !== 'string') ? this.unbind(events, handler) : this.undelegate(selector, events, handler);
};
// Stub in "on/off" for jQuery >= 1.4.3 and < 1.7
if (!$.fn.on && $.fn.delegate && ($.fn.jquery !== '1.4.2')) {
$.fn.on = function(events, selector, data, handler){
var $collection = this;
var useBind = $.type(selector) !== 'string';
// Handle either a string of event names or a map of events-and-callbacks for the 1st parameter
if ($.isPlainObject(events)) {
if (useBind) {
return $collection.bind(events);
}
if ($.isPlainObject(data)) {
$.each(events, function(eventName, callback){
$collection.delegate(selector, eventName, data, callback);
});
return $collection;
}
return $collection.delegate(selector, events);
}
if (useBind) {
return $.fn.bind.apply($collection, arguments);
}
return $collection.delegate(selector, events, data, handler);
};
$.fn.off = function(events, selector, handler){
var $collection = this;
if ($.isPlainObject(events)) {
$.each(events, function(eventName, callback){
undelegateOrUnbind.apply($collection, [eventName, selector, callback]);
});
return $collection;
}
return undelegateOrUnbind.apply($collection, arguments);
};
}
})(jQuery);