Talk about the binding event method of bind(), live(), delegate(), on() in jquery
1. Bind()
$(selector).bind(event,data,function)
Event: Required item; one or more events added to the element.
Data: optional; parameters to be passed
Function: Required; a function that needs to be executed when a binding event occurs;
Define events:
$(selector).bind({event1:function, event2:function, ...});
()
$(selector).live(event,data,function)
Event: Required item; one or more events added to the element
Data: optional; parameters to be passed
Function: Required; a function that needs to be executed when a binding event occurs;
Define events:
$(selector).live({event1:function, event2:function, ...})
()
$(selector).delegate(childSelector,event,data,function)
childSelector: Required item; Elements that need to be added to the event handler, generally a child element of the selector;
event: Required item; one or more events added to the element
Data: optional; parameters to be passed
Function: Required; a function that needs to be executed when a binding event occurs;
Define events:
$(selector).delegate(childselector,{event1:function, event2:function, ...})
()
$(selector).on(event,childselector,data,function)
childSelector: Required item; Elements that need to be added to the event handler, generally a child element of the selector;
event: Required item; one or more events added to the element
Data: optional; parameters to be passed
Function: Required; a function that needs to be executed when a binding event occurs;
Define events:
$(selector).on({event1:function, event2:function, ...},childselector);
The similarities and differences and advantages and disadvantages of the four methods
Similarities:
1. They all support the binding of unit multiple events; space separation method or brace replacement method;
2. All pass events to the document for event response through event methods;
Compare:
The () function can only set events for existing elements; however, live(), on(), delegate() all support event settings for newly added elements in the future;
The () function was highly praised before jquery version 1.7. After version 1.7 came out, the official no longer recommended using bind(), and the replacement function is on(). This is also a newly added function in version 1.7. It can also be used instead of live() function. The live() function has been deleted in version 1.9;
The () function and the delegate() function are similar, but the live() function is worse than the delegate() in terms of execution speed, flexibility and CSS selector support.
The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!