如何在 jQuery 中阻止事件冒泡?
如何停止 jQuery 中的自定义事件冒泡?
例如,我有这样的代码:
$('.myclass').bind('amodaldestroy', function(){
....does something.....
})
如何只允许在冒泡时找到的第一个元素上触发一次?我可以只添加 return false
吗?
$('.myclass').bind('amodaldestroy', function(){
....does something.....
return false;
})
How do I stop custom event bubbling in jQuery?
For example I have this code:
$('.myclass').bind('amodaldestroy', function(){
....does something.....
})
How do I only allow this to be triggered once on the first element it finds when bubbling? Can I just add return false
?
$('.myclass').bind('amodaldestroy', function(){
....does something.....
return false;
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 jQuery 的 文档:
According to jQuery's documentation:
使用
event.stopPropagation();
也可以使用
return false
但两者之间有一个微妙的区别,即返回 false 也会调用event.preventDefault();
Use
event.stopPropagation();
You can also use
return false
but there is a subtle difference between the two in that returning false also callsevent.preventDefault();
对于 IE < 的支持9:
For support in IE < 9:
这可能不是那么好,但是
return false
会同时执行stopPropagation
和event.preventDefault...
如果你只想要一个,你可以根据情况选择根据您的选择
,请阅读此内容,这是来自 SO,只有
return false
才能有效地阻止Default 和 stopPropogation。preventDefault
将阻止默认事件发生,stopPropogatio
n 将阻止事件冒泡,而 return false 将同时执行这两种操作。This might not be that great but
return false
will do bothstopPropagation
andevent.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,stopPropogatio
n will prevent the event from bubbling up and return false will do both.http://api.jquery.com/event.stopPropagation/
http://api.jquery.com/event.stopPropagation/