我有一個網頁中的2個彈出式模式,如果用戶點擊模式外的任何地方,我想關閉它。問題是隻有第一個作品第一個沒有。Java腳本雙模式無法關閉
// 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";
}}
我知道我重寫了代碼。但是我有兩個變量modal1和modal2,我該怎麼樣關閉兩個模態?你能提供一個代碼片段嗎? –