我在网页中有 2 个弹出模式,如果用户单击模式外的任何地方,我想将其关闭。问题是只有第二个有效,第一个无效。
// When the user clicks anywhere outside of the modal1, close it
window.onclick = function(event){
if (event.target == modal1) {
modal1.style.display = "none";
}}
//When the user clicks anywhere outside of the modal2, close it
window.onclick = function(event) {
if (event.target == modal2) {
modal2.style.display = "none";
}}
最佳答案
对两个弹出模式使用相同的点击功能。
window.onclick = function(event){
if (event.target == modal1)
{ modal1.style.display = "none"; }
if (event.target == modal2)
{ modal2.style.display = "none"; }
}
关于javascript - Javascript双模式无法关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42178199/