2 回答
TA贡献1719条经验 获得超6个赞
当您在模态窗口外部单击时,事件传播数组中不应包含模态窗口
换句话说,利用 event.path 属性
工作示例(基于提供的小提琴)
window.addEventListener('click', function(e) {
const allModals = document.querySelectorAll('.project-card');
if (!e.path.some(x => x.className && x.className.includes('project-card'))) {
allModals.forEach(x => x.style.display = 'none');
}
}, true)
工作小提琴示例: https: //jsfiddle.net/7pzs1042/
TA贡献1934条经验 获得超2个赞
window.addEventListener('click', function(e) {
const allModals = document.querySelectorAll('.project-card');
//e.path is deprecated
// use instead e.composedPath() like this:
let paths = e.composedPath()
if (!paths.some(x => x.className && x.className.includes('project-card'))) {
allModals.forEach(x => x.style.display = 'none');
}
}, true)
添加回答
举报