Several ways to prevent event bubbling in JQuery and their differences
- 2020-03-30 01:21:38
- OfStack
JQuery provides two ways to prevent events from bubbling.
Method 1: event.stoppropagation ();
Method 2: return false;
But there is a difference. Return false not only prevents the event from bubbling up, but also prevents the event itself.
Event.stoppropagation () stops the event from bubbling up, not the event itself.
Method 1: event.stoppropagation ();
$("#div1").mousedown(function(event){
event.stopPropagation();
});
Method 2: return false;
$("#div1").mousedown(function(event){
return false;
});
But there is a difference. Return false not only prevents the event from bubbling up, but also prevents the event itself.
Event.stoppropagation () stops the event from bubbling up, not the event itself.