Original: The difference and use of bind (), Live () in jquery (event handling)
Using jquery for a while, when you start to look at someone else's source code, you have doubts about how the event is bound. Like what:
var btn=$ ("#button"); Btn.click (function() { alert ("I have been clicked!" );
This binds a click event.
1.bind () method parsing
But later found that JQUERYAPI also has the Bind method, has not been used, to see how the API use:
function () { alert (this). Text ());});
Binding multiple event types at the same time
function () { $ (this). Toggleclass (' entered ');});
Note: The event type name is separated by a space, using the scene: Multiple event types are used for the same action.
The bind () method can bind multiple events relative to the direct registration event, and the same is all binding on the target object, this method is the longest, and it also solves the browser compatibility problem in event processing.
Note If the parent layer has binding events, you need to block bubbles, e.stoppropagation (), and e.cancellbubble ().
2.live () method parsing
The usage is as follows:
function () { $ (this). Toggleclass (' entered');}); The/.live () method uses the concept of an event delegate to handle the binding of an event.
The Live () method uses the concept of event delegates to handle the binding of events. It is the same as binding an event with. bind (). The difference is:
Live () Always find the top-level element (document) of the event object (Event.target), and event binding on the document node, and the event-bubbling mechanism for event delegation, using only one definition at a time to meet all of the required event objects, when using environments where multiple objects do the same type of thing For example, to have all the elements pop up their ID code as follows:
<DivID= "Parent"> <DivID= "Son"> <DivID= "grandson">fffffffff</Div> </Div> </Div>
We're going to have three Div pop up their IDs now.
The JS code is as follows:
$ ("#parent"). Live ("Click", Function () { alert (this). id); })
The difference and use of bind (), Live () in jquery (event handling)