javascript - 防止子执行附加到容器的函数
问题描述
我有这个删除弹出 div
<div class="delete-popup">
<div class="popup-icon-box">
<img src="icon/delete-icon.svg" alt="delete-icon" class="popup-icon" />
</div>
<h4 class="heading">Kamu yakin ingin menghapus?</h4>
<p class="popup-text">Setiap buku yang telah dihapus tidak dapat dikembalikan lagi</p>
<div class="popup-confirmation-button">
<button class="btn btn--outline no-opacity">Cancel</button>
<button class="btn btn--red">Delete</button>
</div>
</div>
这是我附在文件中的事件代表团
document.addEventListener("click", function (e) {
if (isPopupShowed) {
if (!e.target.classList.contains("delete-popup")) {
deletePopup.classList.remove("show-popup");
deletePopup.classList.add("close-popup");
isPopupShowed = false;
deletePopup.addEventListener("animationend", function () {
deletePopup.classList.remove("close-popup");
});
}
}
if (e.target.classList.contains("delete")) {
deletePopup.classList.add("show-popup");
isPopupShowed = true;
}
});
在if(isPopupShowed)内部,我检查用户是否在弹出容器外部单击,然后弹出窗口将关闭,但是如果我单击容器本身内部的元素(例如标题/图标/等),它也会关闭弹出容器。我如何防止这种情况发生?我尝试使用 stopPropagation 但它似乎不起作用。
解决方案
推荐阅读
- c++ - 为什么我的动作都没有显示在动作编辑器中?
- c# - 为什么 Distinct() 方法不起作用?
- python - 使用for循环(python)生成多个line.set_ydata
- javascript - 如何在 next.js webapp 中将视频等静态文件放在 auth 后面?
- python - 检测文件夹内容是否匹配文件和文件夹模式
- python - 每当我在 vs code 中打开终端时,我的虚拟环境都会自动开始运行
- c# - 单击时如何在整个屏幕上显示图像?
- java - 内存中的对象图序列化
- selenium - 如何在 Ubuntu 中使用 Selenium 中的 chromedriver 打开 Chrome 浏览器
- reactjs - 在反应钩子中将元素添加到现有数组