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>
जे एस:
$(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()
केवल करता है अपना जादू एक बार घटना पहले से ही bubbled है, इसलिए प्रचार से घटना को रोकने बेकार है - यह बहुत देर हो चुकी है, यह पहले से ही पेड़ के शीर्ष तक पहुंच चुकी है और प्रचारित है ...
संबंधित प्रश्न (हुड के तहत): http://stackoverflow.com/questions/1687296/what-is-dom-event-delegation –