[英]What is wrong with this javascript slider?
this is my first time try javascript this image slider is not working and I don't know what is wrong this is what I tried to do https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_self 这是我第一次尝试使用javascript,此图像滑块不起作用,我不知道这是什么错误,这是我尝试执行的操作https://www.w3schools.com/w3css/tryit.asp?filename=tryw3css_slideshow_self
var slideIndex = 1; showDivs(slideIndex); function plusDivs(n) { showDivs(slideIndex += n); } function showDivs (n) { var i; var x = document.getElementByClassName("mySlides"); if (n > x.length) {slideIndex = 1} if (n < 1) {slideIndex = x.length;} for (i = 0 ,i < x.length ,i++) { x[i].style.display = "none"; } x[slideIndex-1].style.display = "block"; }
.container { width:980px; position: relative; } .container img { width: 100%; } button { position: absolute; color: black; background: white; width: 50px; height: 50px; } .left { top:50%; left: :0; } .right { top:50%; right: 0; }
<div class="container"> <img class="mySlides" src= "images/img_fjords.jpg"> <img class="mySlides" src="images/img_forest.jpg"> <img class="mySlides" src="images/img_lights.jpg"> <button class="left" onclick = "plusDivs(-1)">❮</button> <button class="right" onclick = "plusDivs(+1)">❯</button> </div>
You need to change your for to 您需要将其更改为
for (i = 0; i < x.length; i++) {
instead of: 代替:
for (i = 0, i < x.length, i++) {
(use ;
instead of ,
). (使用;
代替,
)。
and change getElementByClassName
to getElementsByClassName
(notice the s at the end of Elements). 并将getElementByClassName
更改为getElementsByClassName
(注意Elements末尾的s)。
var slideIndex = 1; showDivs(slideIndex); function plusDivs(n) { showDivs(slideIndex += n); } function showDivs (n) { var i; var x = document.getElementsByClassName("mySlides"); if (n > x.length) {slideIndex = 1} if (n < 1) {slideIndex = x.length;} for (i = 0; i < x.length; i++) { x[i].style.display = "none"; } x[slideIndex-1].style.display = "block"; }
.container { width:980px; position: relative; } .container img { width: 100%; } button { position: absolute; color: black; background: white; width: 50px; height: 50px; } .left { top:50%; left: :0; } .right { top:50%; right: 0; }
<div class="container"> <img class="mySlides" src= "http://placehold.it/500"> <img class="mySlides" src="http://placehold.it/501"> <img class="mySlides" src="http://placehold.it/502"> <button class="left" onclick = "plusDivs(-1)">❮</button> <button class="right" onclick = "plusDivs(+1)">❯</button> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.