gpt4 book ai didi

jquery - 防止子级单击触发父级单击事件

转载 作者:行者123 更新时间:2023-12-03 22:12:28 25 4
gpt4 key购买 nike

我有一个选择器,它绑定(bind)了一个单击事件,该事件将删除弹出窗口。但是,我只希望选择器处理单击,而不是选择器的子级能够触发单击事件。

我的代码:

<div id="popup">
<div class="popup-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
</div>

当点击.popup-content时,当我不希望#popup的子级这样做时,它会触发点击事件。

jQuery 代码:

$('#popup').bind('click', function()
{
$(this).remove();
});

最佳答案

#popup的事件处理程序中检查e.target == this。即:

$('#popup').bind('click', function(e) {
if(e.target == this) $(this).remove();
});

这样做比将额外的点击处理程序绑定(bind)到所有子级要容易得多。

关于jquery - 防止子级单击触发父级单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6929198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com