我有一个绑定(bind)到许多元素的点击函数。有时这些元素可能会相互结合。因此,单击事件绑定(bind)到子级,也绑定(bind)到其父级。该方法特定于单击的元素。当然,由于事件冒泡,子级的事件首先被触发,然后才是父级的事件。我不能同时调用它们,因为 parent 的事件会覆盖 child 的事件。所以我可以使用 event.stopPropagation() ,这样只有单击的第一个元素才会收到事件。问题是该元素还附加了其他单击事件,例如,我在这些元素上使用 jQuery 的可拖动。如果我停止传播点击事件,那么draggable将不起作用,并且不会调用以下点击事件。
所以我的问题是:有没有办法停止事件将调用的方法而不是整个事件的事件冒泡?
聪明的约翰,但问题是:
<div id="Elm1"><!-- relative -->
<div class="Elmchildren"></div><!-- absolute-->
<div class="Elmchildren"></div><!-- absolute-->
<div class="Elmchildren"></div><!-- absolute-->
<div id="Elm2"><!-- relative -->
<div class="Elmchildren"></div><!-- absolute-->
<div class="Elmchildren"></div><!-- absolute-->
<div class="Elmchildren"></div><!-- absolute-->
</div>
</div>
点击事件绑定(bind)到#Elm1
和#Elm2
。 .Elmchildren
的宽度和高度均为 100%。所以他们实际上是当前的目标。
最佳答案
尝试这样的事情
$(mySelector).click(function(evt) {
if (evt.target == evt.currentTarget) {
///run your code. The if statment will only run this click event on the target element
///all other click events will still run.
}
});
关于javascript - 如何停止传播绑定(bind)函数而不是整个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2977552/