例如
- <div class="container">
- <div class="inside">I am not fire when click me</div>
- </div>
- $('.container').click(function(){
- // container do something here
- });
但是,当我单击其中的div时,它也会触发容器的click事件,因为div位于容器内部,所以,当我点击内部div时,我需要一种方法来阻止容器事件触发!
非常感谢你!!
解决方法
作为更通用的解决方案,您应该检查容器的单击事件处理程序中的
e.target
.
- $('.container').click(function(e) {
- // If you want to ignore clicks to anything inside the `.container` div:
- if (!$(e.target).hasClass('container')) return;
- // or,if you only want the `.inside` div to not fire the event,if ($(e.target).hasClass('inside')) return;
- // container do something here
- });
这样你就不会阻止事件的传播,这会破坏绑定的live
处理程序.