我有一个DIV与一个分类的foobar,和一些DIV内的DIV是未分类的,但我认为他们继承了foobar类:
$('.foobar').on('click', function() { /*...do stuff...*/ });
我希望它只在点击DIV的某个地方时发射,而不是在它的子DIV上。
我有一个DIV与一个分类的foobar,和一些DIV内的DIV是未分类的,但我认为他们继承了foobar类:
$('.foobar').on('click', function() { /*...do stuff...*/ });
我希望它只在点击DIV的某个地方时发射,而不是在它的子DIV上。
当前回答
你可以使用event. currentarget。它只会点击得到事件的元素。
目标= e => 控制台日志(e . currentTarget); 的; <ul onClick={target}类 < li > < p > <i类 < / p > < / li > < /德>
其他回答
你可以使用event. currentarget。它只会点击得到事件的元素。
目标= e => 控制台日志(e . currentTarget); 的; <ul onClick={target}类 < li > < p > <i类 < / p > < / li > < /德>
你可以用冒泡来帮助自己:
$('.foobar').on('click', function(e) {
// do your thing.
}).on('click', 'div', function(e) {
// clicked on descendant div
e.stopPropagation();
});
我的情况类似,但这是当你有几个foobar-s,你想关闭只有一个-每次点击:
查找父case
$(".foobar-close-button-class").on("click", function () {
$(this).parents('.foobar').fadeOut( 100 );
// 'this' - means that you finding some parent class from '.foobar-close-button-class'
// '.parents' -means that you finding parent class with name '.foobar'
});
查找子案例
$(".foobar-close-button-class").on("click", function () {
$(this).child('.foobar-close-button-child-class').fadeOut( 100 );
// 'this' - means that you finding some child class from '.foobar-close-button-class'
// '.child' -means that you finding child class with name '.foobar-close-button-child-class'
});
我有同样的问题,并提出了这个解决方案(基于其他答案)
$( ".newsletter_background" ).click(function(e) {
if (e.target == this) {
$(".newsletter_background").hide();
}
});
基本上,如果目标是div则运行代码否则什么都不做(不要隐藏它)
如果您不介意只针对较新的浏览器,还有另一种方法。只需要添加CSS
pointer-events: none;
到您想要捕获点击的div的任何子div。这是支持表
http://caniuse.com/#feat=pointer-events