【发布时间】:2021-01-09 07:04:35
【问题描述】:
https://alexkchapman.com/Vans.html
在我网站上的三个图像中的任何一个(以及移动设备上的导航按钮)上,第一次点击都不起作用,但后续点击可以。控制台正在记录单击已注册,但图像未更改。
下面是相关的js:
var slideIndex = [1, 1, 1];
/* Class the members of each slideshow group with different CSS classes */
var slideId = ["slides1", "slides2", "slides3"];
showSlides(1, 0);
showSlides(1, 1);
showSlides(1, 2);
function plusSlides(n, no) {
showSlides(slideIndex[no] += n, no);
console.log(n);
}
function showSlides(n, no) {
var i;
var x = document.getElementsByClassName(slideId[no]);
if (n > x.length) {
slideIndex[no] = 1
}
if (n < 1) {
slideIndex[no] = x.length
}
for (i = 0; i < x.length; i++) {
x[i].style.display = "none";
}
x[slideIndex[no] - 1].style.display = "block";
}
【问题讨论】:
-
请在此处发布minimal reproducible example,而不是在外部站点(当您得到问题的答案时可能会修复,因此对未来的读者毫无用处)。
-
它还有助于详细说明重现问题的确切步骤
-
我不知道这是否相关,但您需要将初始调用放在
showSlides()中的window.onload函数中。您在幻灯片加载到 DOM 之前调用它们,因此尝试设置x[0]的样式时会出错。 -
我不明白
n是如何被使用的。在plusSlides()中,它是增加幻灯片索引的数量。但是在showSlides()中,您将它与类中的元素数量进行比较。为什么要将增量与总数进行比较? -
你可能只想要
if (slideIndex[no] > x.length) { slideIndex[no] = 1; }
标签: javascript html css console