我有一个可以显示或不显示的div。当它显示时我想将显示传递给none,如果你点击不在该div中的某个地方。所以我做的是:
document.addEventListener('click',closeDiv)
document.getElementById('myDiv').addEventListener('click',stopPropagation)
function closeDiv(){
let div = document.getElementById('myDiv')
div.style.display='none'
}
但即使我点击了div,显示仍然没有
答案 0 :(得分:0)
找到解决方案:
document.addEventListener('click',closeModal)
document.getElementById('myDiv').addEventListener('click',stopPropagation)
function stopPropagation(e){
e.stopPropagation();
}
function closeDiv(){
let div = document.getElementById('myDiv')
div.style.display='none'
}
答案 1 :(得分:-1)
如果你制作window.addEventListener('click', outDiv);
和这个功能:
let myDiv = document.querySelector('myDiv');
function outDiv (event) {
if (!myDiv.classList.contain(event.target)) {
// Make Something
}
}