Home  >  Article  >  Web Front-end  >  In-depth understanding of the difference between live and bind methods in jQuery_jquery

In-depth understanding of the difference between live and bind methods in jQuery_jquery

WBOY
WBOYOriginal
2016-05-16 17:08:34943browse

Note that if you add layers and objects through jq, you must use live(), anything else will not work

The disadvantage of live is that it does not release space after running, and too much use will occupy more memory. bind() releases space after clicking

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 will pop up. Then, imagine that another element is added after this.

Although this new element can also match the selector ".clickme", since this element is added after calling .bind(), clicking on this element will have no effect.

.live() provides a method for this situation. If we bind the click event like this:

$('.clickme').live('click', function() {
alert("Live handler called.");
});

Then add a new element:

$('body').append('

Another target
');

Then click on the newly added element, he can still trigger the event handler 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, mouseover, and mouseup in jQuery 1.3. In jQuery In 1.4.1, focus and blue events are even supported (mapped to focusin and focusout, which are more suitable and can bubble up). In addition, in jQuery 1.4.1, hover (mapped to "mouseenter mouseleave") is also supported.

(2) live() does not fully support elements found through DOM traversal. Instead, you should always use the .live() method directly after a selector.

(3) When an element uses the live method to bind events, if you want to prevent the delivery or bubbling of events, you must return false in the function. Simply calling stopPropagation() cannot prevent the delivery of events. Or bubbling

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn