在站點上測試多個模塊。兩種模式都可以打開。但是,第二個模態(contmodal)不會關閉。這似乎也造成了導致其他JS破裂的問題。使用JS關閉多個模塊
//* Get the Newsletter Modal
var newsmodal = document.getElementById('newsletter-modal');
var contmodal = document.getElementById('contact-modal');
// Get the button that opens the modal
var newsbtn = document.getElementsByClassName("signup-button")[0];
var editbtn = document.getElementsByClassName("contact-button")[0];
// Get the <span> element that closes the modal
var modalspan = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
newsbtn.onclick = function() {
newsmodal.style.display = "block";
}
editbtn.onclick = function() {
contmodal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
modalspan.onclick = function() {
newsmodal.style.display = "none";
contmodal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == newsmodal) {
newsmodal.style.display = "none";
}
if (event.target == contmodal) {
contmodal.style.display = "none";
}
}
代碼似乎沒有問題。你有沒有看到在控制檯中是否有任何錯誤? – Alex
看看這個https://github.com/EasonWang01/react-pure-css-modal 不需要使用js – seasonqwe
問題出在哪裏當用戶點擊(x)時,關閉模式?或者當用戶點擊模式外的任何地方時,關閉它? –