我似乎不能让它停止传播..
$(document).ready(function(){
$("body").on("click","img.theater",function(event){
event.stopPropagation();
$('.theater-wrapper').show();
});
// This shouldn't fire if I click inside of the div that's inside of the
// `.theater-wrapper`, which is called `.theater-container`, anything else it should.
$(".theater-wrapper").click(function(event){
$('.theater-wrapper').hide();
});
});
发布于 2012-01-25 06:14:17
因为您是在body
元素上使用on
,而不是直接在img.theater
上使用,所以事件会冒泡到body
元素,这就是它的工作方式。
在事件冒泡的过程中,.theater-wrapper
元素click
事件将被触发,所以您可以看到它。
如果您没有创建任何动态元素,那么直接在img.theater
元素上附加单击事件处理程序。
$("img.theater").click(function(event){
event.stopPropagation();
$('.theater-wrapper').show();
});
或者,您可以在.theater-wrapper
elements click
处理程序中检查click
事件的目标,然后什么也不做。
$(".theater-wrapper").click(function(event){
if ($(event.target).is('img.theater')){
event.stopPropagation();
return;
}
$('.theater-wrapper').hide();
});
发布于 2012-10-15 21:21:08
最好的方法是:
$(".theater-wrapper").click(function(event){
if (!$(event.target).is('img.theater')){
$('.theater-wrapper').hide();
}
});
使用手风琴和复选框,对我来说很有效
发布于 2019-01-04 17:52:35
使用event.stopImmediatePropagation()它对我很有效
https://stackoverflow.com/questions/8995008
复制相似问题