0

我在网页中有 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";
}}
4

3 回答 3

0

对两种弹出模式使用相同的点击功能。

window.onclick = function(event){
if (event.target == modal1)    
{ modal1.style.display = "none"; }
if (event.target == modal2)
{ modal2.style.display = "none"; }
} 
于 2017-02-17T04:56:57.883 回答
0

您正在用第二个 onclick 覆盖第一个 onclick。

只需单击一次窗口即可检查模式类型,并根据您设置的模式显示无。

于 2017-02-11T16:26:43.553 回答
0

您正在用第二个 onclick 覆盖第一个 onclick。您可以这样做;

window.onclick = function(event){
 if (event.target == modal1){modal1.style.display = "none"; } 
 if (event.target == modal2){modal2.style.display = "none";}
}
于 2017-02-17T05:02:36.880 回答