Differences between the live and bind methods in jQuery
This article mainly analyzes the differences between the live and bind methods in jQuery in detail. If you need them, you can refer to them and hope to help you.
Note: If the layer and object added through jq must use live (), none of them work.
The disadvantage of live is that it does not release space after running. Too much use will occupy more memory, and bind () will release space after clicking it.
Difference 1:
Click here
You can bind a simple click event to this element:
$ ('. Clickme'). bind ('click', function (){
$ ('Body'). append ('
Another target
');
});
When an element is clicked, a warning box is displayed. Then, imagine that another element was added.
Although this new element can also match the selector ". clickme", clicking this element will not work because it is added after. bind () is called.
. Live () provides a method for this situation. If we bind a click event like this:
$ ('. Clickme'). live ('click', function (){
Alert ("Live handler called .");
});
Then add a new element:
$ ('Body'). append ('
Another target
');
Then, click the new element to trigger the event processing function.
Difference 2:
(1) The bind method can bind any JavaScript event, while the live method only supports click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, and mouseover in jQuery1.3, and mouseup. in jQuery 1.4.1, the focus and blue events are even supported (ing to focusin and focusout that are more appropriate and can be bubbling ). In addition, hover (mapped to "mouseenter mouseleave") is also supported in jQuery 1.4.1 ").
(2) live () does not fully support elements found through DOM traversal. Instead, the. live () method should always be used directly after a selector.
(3) When an element binds an event using the live method, if you want to prevent the event from being transmitted or bubbling, you must return false in the function and only call stopPropagation () it is impossible to prevent the passing or bubbling of events.