Understand the difference between the live and bind methods in jQuery

  • 2020-03-30 00:58:06
  • OfStack

Note that you must use live () if you are adding layers and objects through jq; nothing else works

The downside of live is that it doesn't free up space after running, and too much use takes up more memory, whereas bind () frees up space after clicking

A difference:

Click here

You can bind a simple click event to this element:

$(' clickme '). The bind (' click ', function () {
$(" body "). Append ('

Another target

When an element is clicked, a warning box pops up. Then, imagine that there's another element added after that.

Although this new element also matches the selector ".clickme", clicking on it will have no effect because it was added after the.bind() call.

.live() provides a method for this. 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 new element and it will still trigger the event handler.


Difference between 2:

(3) when an element USES the live method for event binding, if it wants to prevent the event from passing or bubbling, it is necessary to return false in the function. Just calling stopPropagation() cannot prevent the event from passing or bubbling

Related articles: