为什么我的幻灯片开头没有显示?

时间:2017-05-17 05:20:17

标签: javascript html

我幻灯片播放工作,我按箭头但加载时会出现此错误

  

未捕获的TypeError:无法读取未定义的属性“样式”       在showSlides(gallery.js:24)       在gallery.js:2

这是我的代码 的Javascript

var slideIndex = 1;
showSlides(slideIndex);

function plusSlides(n) {
  showSlides(slideIndex += n);
}

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {
     slideIndex = 1
  } 
  if (n < 1) {
   slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none"; 
  }
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block"; 
  dots[slideIndex-1].className += " active";
}

1 个答案:

答案 0 :(得分:0)

很可能是因为元素slides[slideIndex-1]不存在,可能是因为document.getElementsByClassName("mySlides")返回undefinedslideIndex-1 < 0