我在网页中有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";
}}
答案 0 :(得分:0)
你用第二个onclick覆盖了第一个onclick。
只需在窗口上点击一下,检查模态类型,并根据您设置它显示无。
答案 1 :(得分:0)
对两个弹出模式使用相同的点击功能。
window.onclick = function(event){
if (event.target == modal1)
{ modal1.style.display = "none"; }
if (event.target == modal2)
{ modal2.style.display = "none"; }
}
答案 2 :(得分:0)
你用第二个onclick覆盖了第一个onclick。你可以这样做;
window.onclick = function(event){
if (event.target == modal1){modal1.style.display = "none"; }
if (event.target == modal2){modal2.style.display = "none";}
}