Problem with live event

I just read http://api.jquery.com/event.stopPropagation/

Since the .live () method handles events when they propagate to the top of the document, it is not possible to stop the distribution of live events

I was a little confused by this statement, can someone please explain the same to me with some example?

+5
source share
2 answers

The Live method binds the handler to the document and determines which element raised the event from the property event.target.

Thus, the actual handler is at the top (in terms of hierarchy).

stopPropagation DOM, ( .live), ..


..

- document
  - div
    - link

( bind click).

, , , , click DOM , , , div . ( .stopPropagation)

, .live , . , ( , ), , DOM ( , ). , . , stopPropagation .

+4

HTML:

<div id="outer">
   <div id="inner">
       <span>.live() version</span>
   </div>
</div>

<div id="outer2">
   <div id="inner2">
       <span>.delegate() version</span>
   </div>
</div>

JS:

$(function(){

   $('#inner2').delegate('span', 'click', function(e){
      e.stopPropagation(); // indeed, no alert!
   });

   $('span').live('click', function(e){
      e.stopPropagation();
      // we would expect the propagation to stop here, so no alert, right?
   });

   $('#outer2, #outer').click(function(){ alert("Don't reach me!"); });

});

: http://jsfiddle.net/knr3v/2/

.live() , , - , ...

+2

All Articles