Home > Article > Web Front-end > Usage and difference analysis of bind, live, delegate and one methods in jQuery_jquery
The
bind() method is used to attach a handler to the event of each matching element and return a jQuery object.
.bind(eventType[, evnetData], Handler(eventObject))
Among them, the parameter eventType is a string containing one or more javaScript event types, such as click, submit or the name of a custom event. When specifying multiple event types, separate each type with a space; eventData is a Map type, giving Output the data to be passed to the event handler, handler specifies the function to be executed when the event is triggered, and eventObject represents the event object.
The.bind() method attaches an event handler to the eventType event for each element in the set of matching elements, passing data to the event handler if necessary.
Thelive() method attaches an event handler to the specified event for all elements matching the current selector (including existing or future additions) and returns a jQuery object.
.live(eventType,[eventData],handler)
Among them, the parameter eventType is a string containing one or more javaScript event types, such as click, keydown or the name of a custom event. eventData is an optional parameter, which is a Map type and is given to be passed to the event handler. Data, this parameter was added in jQuery 1.4; handler is a function, these functions will be executed when the event is triggered
The.live() method attaches an event handler to the eventType event of each matching element (including currently existing and future additions). If necessary, you can also use eventData to pass data to the event handler.
The.live() method is a variant of the basic .bind() method that attaches event handlers to elements. When .bind() is called, the element matching the jQuery object will have the event attached to it. handler, but elements added later will not have the event handler attached, so the .bind() method needs to be called again on these elements.
The.one() method attaches an event handler to the specified event of the matching element and returns a jQuery object. The attached event handler can only be executed at most once.
.one(eventType,[eventData],handler(eventObject))
Among them, the parameter eventType is a string containing one or more javaScript event types, such as click, submit or the name of a custom event. When specifying multiple event types, separate each type with a space; eventData is a Map type, giving Output the data to be passed to the event handler, handler specifies the function to be executed when the event is triggered, and eventObject represents the event object.
.one() method is similar to .bind(), except that event handlers bound using .one() will be automatically unbound after being executed once.
The.delegate() method attaches a handler to one or more events for all elements (existing or future) that match the selector, based on a specific set of root elements.
.delegate(selector,eventType[,eventData],handler)
Among them, the parameter selector is a selector used to filter elements that trigger events; eventType is a string specifying one or more JavaScript event types (multiple events are separated by spaces), such as click, keydown or custom Event name; eventData is a mapping type, representing the data to be passed to the event handler; handler represents the function executed when the event is triggered.
.delegate() is similar to .live() and can delegate the binding of each event to the specified DOM element.