1 回答

TA貢獻1802條經驗 獲得超10個贊
您的關閉功能span.onclick = function()不會隱藏子項,因此第二次單擊時,您會看到上次單擊時的上一個圖像和 modalImage。
function myBeautifulFunc(imageId, modalImageId) {
var modal = document.getElementById('myModal');
var img = document.getElementById(imageId);
var modalImg = document.getElementById(modalImageId);
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
img.style.display = "none" // hide this also
modalImg.style.display = "none" // hide this also
}
}
在任何點擊之前
.myModal = 隱藏
img1 = 隱藏
modalImg1 = 隱藏
第一次點擊后關閉
.myModal = 隱藏
img1 = 可見
modalImg1 = 可見
第二次點擊后
.myModal = 隱藏
img1 = 可見
modalImg1 = 可見
img2 = 可見
modalImg2 = 可見
添加回答
舉報