gpt4 book ai didi

javascript - Javascript双模式无法关闭

转载 作者:行者123 更新时间:2023-11-28 04:21:40 25 4
gpt4 key购买 nike

我在网页中有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com