jquery version: 2.1.4
- Most commonly used event binding bind, called on.
1 function (types, data, fn) {2 return This NULL , data, FN); 3 }
The corresponding unbind Unband, called off.
1 function (types, fn) {2 return This NULL , FN); 3 }
- Only one binding is executed once, naked call on.
1 function (types, selector, data, fn) {2 return this. On (types, selector, data, FN, 1 ); 3 }
- Delegate delegate, call on
1 function (selector, types, data, fn) {2 return This . On (types, selector, data, FN); 3 }
Undelegate Call off
1 function (selector, types, fn) {2 // (namespace) or (selector, types [, fn]) 3 return This this. Off (types, selector | | "* *", FN); 4 }
- Finally uncover the veil of on.
Parameters:
- Types Event Type
- Selector is a child element in a delegate. (The one in the past seems to have no such parameter)
- Data parameter
- FN event handler function
- One is one bound
1Onfunction(types, selector, data, FN,/*INTERNAL*/One ) {2 varorigfn, type;3 4 //Types can be a map of Types/handlers5 if(typeoftypes = = = "Object" ) {6 //(Types-object, selector, data)7 if(typeofSelector!== "string" ) {8 //(types-object, data)9data = Data | |selector;Tenselector =undefined; One } A for(Typeinchtypes) { - This. On (type, selector, data, types[type], one); - } the return This; - } - - if(Data = =NULL&& fn = =NULL ) { + //(types, fn) -fn =selector; +data = Selector =undefined; A}Else if(fn = =NULL ) { at if(typeofselector = = = "string" ) { - //(types, selector, fn) -fn =data; -data =undefined; -}Else { - //(types, data, FN) infn =data; -data =selector; toselector =undefined; + } - } the if(fn = = =false ) { *fn =Returnfalse; $}Else if( !fn) {Panax Notoginseng return This; - } the + if(One = = 1 ) { AORIGFN =fn; thefn =function(event) { + //Can Use an empty set, since event contains the info - jQuery (). Off (event); $ returnOrigfn.apply ( This, arguments); $ }; - //Use same GUID so caller can remove using ORIGFN -Fn.guid = Origfn.guid | | (Origfn.guid = jquery.guid++ ); the } - return This. each (function() {WuyiJQuery.event.add ( This, types, FN, data, selector); the }); -}
on
corresponding Unbind off
1Offfunction(types, selector, fn) {2 varhandleobj, type;3 if(Types && Types.preventdefault &&types.handleobj) {4 //(event) dispatched Jquery.event5Handleobj =types.handleobj;6 jQuery (types.delegatetarget). Off (7Handleobj.namespace? Handleobj.origtype + "." +HandleObj.namespace:handleObj.origType,8 Handleobj.selector,9 Handleobj.handlerTen ); One return This; A } - if(typeoftypes = = = "Object" ) { - //(Types-object [, selector]) the for(Typeinchtypes) { - This. Off (type, selector, types[type]); - } - return This; + } - if(Selector = = =false||typeofselector = = = "function" ) { + //(types [, fn]) Afn =selector; atselector =undefined; - } - if(fn = = =false ) { -fn =Returnfalse; - } - return This. each (function() { inJQuery.event.remove ( This, types, FN, selector); - }); to},
off
jquery Digging Source--event binding