在我的代码中使用样式时,我遇到错误
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中的任何语法更改