我有一个带有类名为foobar
的DIV,这个DIV里面有几个没有类名的DIV,但我认为它们会继承foobar
类:
$('.foobar').on('click', function() { /*...do stuff...*/ });
我希望只有在DIV区域内但不是其子DIV内点击时才触发该事件。
e.target
与this
相同,则表示您没有单击后代元素。
$('.foobar').on('click', function(e) {
if (e.target !== this)
return;
alert( 'clicked the foobar' );
});
.foobar {
padding: 20px; background: yellow;
}
span {
background: blue; color: white; padding: 8px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class='foobar'> .foobar (alert)
<span>child (no alert)</span>
</div>
我没有成功地得到被接受的答案,但是这似乎可以解决问题,至少在纯JS中。
if(e.target !== e.currentTarget) return;
this
更准确,因为指向this
的对象可能会根据调用它的作用域和上下文而发生变化。 - eballesteevent.currentTarget
。 - Mark Carpenter Jrif (e.target === e.currentTarget) { ...your code }
为什么这个代码行得通:
e.target是触发事件的元素(例如,用户点击的元素)
e.currentTarget是事件监听器所附加到的元素。因此,当您比较这两个元素时,事件附加的元素与被单击的元素相同。 - Guilherme Samuel如果你不介意只针对新版浏览器,还有另一种方法可行。只需添加CSS即可。
pointer-events: none;
要捕获点击事件,可以向想要捕获的 div 的任何子元素添加。以下是支持表格
click
的编程点击事件触发被阻止? - Pankaj Parkarinput.focus()
聚焦输入框,但无论如何,对于这个问题有多种解决方案,某人使用哪一个将基于他们的用例。 - hobberwickey您可以利用冒泡事件:
$('.foobar').on('click', function(e) {
// do your thing.
}).on('click', 'div', function(e) {
// clicked on descendant div
e.stopPropagation();
});
<a>
元素的点击。只有一个问题:当我在它们上面中键单击时,事件仍然会触发(在Google Chrome中测试)。是否有一种变体也可以防止这种情况的发生? - cgogolin//bind `click` event handler to the `.foobar` element(s) to do work,
//then find the children of all the `.foobar` element(s)
//and bind a `click` event handler to them that stops the propagation of the event
$('.foobar').on('click', function () { ... }).children().on('click', function (event) {
event.stopPropagation();
//you can also use `return false;` which is the same as `event.preventDefault()` and `event.stopPropagation()` all in one (in a jQuery event handler)
});
这将停止在.foobar
元素的任何子元素上冒泡click
事件,因此事件不会到达.foobar
元素来触发其事件处理程序。
我曾经遇到同样的问题,根据其他答案,我想出了这个解决方案。
$( ".newsletter_background" ).click(function(e) {
if (e.target == this) {
$(".newsletter_background").hide();
}
});
(function(e) {})
吗? - Zaffer$(".advanced ul li").live('click',function(e){
if(e.target != this) return;
//code
// this code will execute only when you click to li and not to a child
})
target = e => {
console.log(e.currentTarget);
};
<ul onClick={target} className="folder">
<li>
<p>
<i className="fas fa-folder" />
</p>
</li>
</ul>
currentTarget
并不决定事件是否被执行,它只是保存元素的引用。 - shaedrichpointer-events: none;
并且目标是现代浏览器,您可以使用composedPath
来检测对象上的直接点击,如下所示:pointer-events: none;
并且目标是现代浏览器,您可以使用composedPath
来检测对象上的直接点击,如下所示:element.addEventListener("click", function (ev) {
if (ev.composedPath()[0] === this) {
// your code here ...
}
})
我的情况类似,但是这种情况是当你有几个foobar
时,并且你只想通过一次点击关闭一个:
$(".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'
});
if
语句的目的是使得在点击.foobar
元素的子元素时,下面的代码不会运行。如果我们将警告放在顶部,它将始终运行。因此,基本上,当单击外部黄色区域时,您应该看到警报,但不应在蓝色子元素上看到。 - user1106925