I have a series of click events that cause me some distribution problems. The data for this container is loaded via ajax, therefore, the body on click method. Here is the code I have:
$(function () {
$("body").on("click","#top-div",function(){
console.log("top event");
});
$("body").on("click","#middle-div",function(e){
e.stopPropagation();
});
$("body").on("click","#bottom-div",function(){
console.log("bottom event");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="top-div">
top
<div id="middle-div">
middle
<div id="bottom-div">
bottom
</div>
</div>
</div>
Run codeHide resultThe top div has an event that the middle does not need to inherit, therefore stopPropagation(). However, the lower part should have its own event, but stopPropagation()from the middle it stops it from executing the event (if I delete the propagation of the stop, the lower event will fire, but by coincidence, as well as the top)
How can I get around this bubble issue?
source
share