gpt4 book ai didi

javascript - [variable].onclick = function () 没有执行,控制台没有错误

转载 作者:行者123 更新时间:2023-11-28 15:01:14 25 4
gpt4 key购买 nike

我是 javascript 的新手,这可能非常简单。我正在尝试创建代码,当我单击图像时,同一图像将以模式启动。这适用于查看完整尺寸图像的作品集。我的 javascript 控制台没有错误,但是当我单击图像时没有任何反应。

JavaScript:

var modal = document.getElementById("myModal");
var srcImg = document.getElementsByClassName("showFull");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
srcImg.onclick = function () {
modal.style.display = "block";
modalImg.src = this.src;
modalImg.alt = this.alt;
captionText.innerHTML = this.alt;
}

var exit = document.getElementsByClassName("close")[0];

exit.onclick = function () {
modal.style.display = "none";
};

html:

<div class="mySlides fade">
<img class="slide showFull" src="images/mikesHarder.jpg alt="caption 123" style="width:100%" />
</div>

<div class="mySlides fade">
<img class="slide showFull" src="images/Going-Green.jpg" alt="caption 123" style="width:100%" />
</div>

<div id="myModal" class="modal">

<!-- The Close Button -->
<span class="close">&times;</span>

<!-- Modal Content -->
<img id="img01" class="modal-content"/>
<div id="caption"></div>
</div>

CSS:

.showFull {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}

.showFull:hover {opacity: 0.9;}

.modal {
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
background-color: rgba(0,0,0,0.8);
}

.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 480px;
}

.modal-content {
animation-name: zoom;
animation-duration: 0.6s;
}

@keyframes zoom {
from {transform:scale(0)}
to {transform:scale(1)}
}

.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}

.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}

@media only screen and (max-width: 700px){
.modal-content {
width: 100%;
}
}

最佳答案

我认为 1 个问题是 srcImg 是一个 html 元素数组而不是单个元素,因此当您编写 srcImg.onclick 时它不起作用,因为您只能对表示 html 元素的 DOM 对象使用 onclick。

关于javascript - [variable].onclick = function () 没有执行,控制台没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50125800/

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