收到TypeError:无法读取未定义反应的属性“样式”

时间:2019-04-13 12:01:26

标签: javascript css

在我的代码中使用样式时,我遇到错误

 TypeError: Cannot read property 'style' of undefined 

我尝试调用showSlides函数。错误引发样式。

slides[this.slideIndex-1].style.display = "block"; 

代码是:

showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");

  if (n > slides.length) {this.slideIndex = 1} 
  if (n < 1) {this.slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  this.slideIndex++;
  if (this.slideIndex > slides.length) {this.slideIndex = 1}    
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[this.slideIndex-1].style.display = "block";  
  dots[this.slideIndex-1].className += " active";
  setTimeout(this.showSlides, 2000); // Change image every 2 seconds  
}

react js中的任何语法更改

0 个答案:

没有答案