How to stop events bubbling in jQuery? [duplicate]

此生再无相见时 提交于 2019-11-26 13:44:41

According to jQuery's documentation:

$('myclass').bind('amodaldestroy'), function(event) {
    ....does something....
    event.stopPropagation();
});

Use event.stopPropagation();

$('.myclass').bind('amodaldestroy', function(e){
    e.stopPropagation();
});

You can also use return false but there is a subtle difference between the two in that returning false also calls event.preventDefault();

For support in IE < 9:

$(".element").click(function(e)
{
    var event = e || window.event;
    event.stopPropagation ? event.stopPropagation() : (event.cancelBubble=true);
});

This might not be that great but

return false;

return false will do both stopPropagation and event.preventDefault...

if you want only one you can choose based on your choice

please read this , this is from SO only

return false is effectively both preventDefault and stopPropogation.

preventDefault will prevent the default event from occuring, stopPropogation will prevent the event from bubbling up and return false will do both.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!