0

I understand that live() in jQuery can be used to bind events to elements which do not exist at the time of binding. However there is also a difference wrt to event propagation between bind() method and the live(). The following is an excerpt from the jQuery in action book:

"Firstly, recognize that live events aren’t native “normal” events. When an event such as a click occurs, it propagates up through the DOM elements as described earlier in this chapter, invoking any event handlers that have been established. Once the event reaches the context used to create the wrapped set upon which live() was called (usually the document), the context checks for elements within itself that match the live selector. The live event handlers are triggered on any elements that match, and this triggered event doesn’t propagate."

Can anyone help me understand what this means?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
newbie
  • 1,049
  • 5
  • 15
  • 29

1 Answers1

0

Think this scenario:

I have several <p> elements.

$('p').bind('click', function(){...});

add some extra <p> (using get(), or html(), anything)

the new <p> don't have any binding!!

of course, since the new <p> didn't exist when you did the $('p')... at step 2, it didn't bind the event handler to them.

now, if you do this:

i have several <p> elements.

$('p').live('click', function(){...});

add some extra <p> (using get(), or html(), anything)

the new <p> do have the binding!!

Note jQuery has deprecated live() since 1.7, instead use on()

Sridhar R
  • 20,190
  • 6
  • 38
  • 35