我创建了一个网站,在该网站中以网格格式显示图像。当用户单击图像时,将显示一个弹出窗口,其中单击的图像带有关闭按钮和下面的文本,下面是我的操作
var modals = document.getElementById("memodals");
// Get the image and insert it inside the modals - use its "alt" text as a captions
var img = document.getElementById("l1");
var modalsImg = document.getElementById("img01");
var captionsText = document.getElementById("captions");
img.onclick = function() {
modals.style.display = "block";
modalsImg.src = this.src;
captionsText.innerHTML = this.alt;
}
var img = document.getElementById("l2");
var modalsImg = document.getElementById("img01");
var captionsText = document.getElementById("captions");
img.onclick = function() {
modals.style.display = "block";
modalsImg.src = this.src;
captionsText.innerHTML = this.alt;
}
var img = document.getElementById("l3");
var modalsImg = document.getElementById("img01");
var captionsText = document.getElementById("captions");
img.onclick = function() {
modals.style.display = "block";
modalsImg.src = this.src;
captionsText.innerHTML = this.alt;
}
var img = document.getElementById("l4");
var modalsImg = document.getElementById("img01");
var captionsText = document.getElementById("captions");
img.onclick = function() {
modals.style.display = "block";
modalsImg.src = this.src;
captionsText.innerHTML = this.alt;
}
// Get the <span> element that closes the modals
var span = document.getElementsByClassName("closes")[0];
// When the user clicks on <span> (x), closes the modals
span.onclick = function() {
modals.style.display = "none";
}
.me {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
.me:hover {
opacity: 0.7;
}
/* The modals (background) */
.modals {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, 0.9);
/* Black w/ opacity */
}
/* modals Content (image) */
.modals-content {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
}
/* captions of modals Image */
#captions {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modals-content,
#captions {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
@-webkit-keyframes zoom {
from {
-webkit-transform: scale(0)
}
to {
-webkit-transform: scale(1)
}
}
@keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
/* The closes Button */
.closes {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.closes:hover,
.closes:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
/* 100% Image Width on Smaller Screens */
@media only screen and (max-width: 700px) {
.modals-content {
width: 100%;
}
}
<div style="margin-left:35px;">
<img class="me" id="l1" src="images\Necklace\03.png" alt="For More Details Contact: 9441394419">
<img class="me" id="l2" src="images\Necklace\06.png" alt="For More Details Contact: 9441394419">
<img class="me" id="l3" src="images\Necklace\08.png" alt="For More Details Contact: 9441394419">
<img class="me" id="l4" src="images\Necklace\10.png" alt="For More Details Contact: 9441394419">
<div style="margin-top:100px;">
当我仅将代码用于一个图像时,它可以正常工作,但是当我像上述代码那样对多个图像进行操作时,则无法正常工作。谁能告诉我我的代码有什么问题,或者请编辑我的代码并向我显示答案