当我把我的html模板转换成javascript中的react时出错

biswetbf  于 2022-11-27  发布在  Java
关注(0)|答案(2)|浏览(160)

错误为未捕获的TypeError:无法读取undefined的属性(阅读'style')
编码:

let slideIndex = 0;
showSlides();

function showSlides() {
  let i;
  let slides = document.getElementsByClassName("mySlides");
  let dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].style.display = "none";  
  }
  slideIndex++;
  if (slideIndex > slides.length) {slideIndex = 1}    
  for (i = 0; i < dots.length; i++) {
    dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";   //Error is showed at this line  
  dots[slideIndex-1].className += " active";
  setTimeout(showSlides, 2000); // Change image every 2 seconds
}

我在公共文件夹的index.html中包含了我的script标记。除此之外,其余javascript文件都正常工作。

zc0qhyus

zc0qhyus1#

常量mystyle = {显示:“块”,};

e4yzc0pl

e4yzc0pl2#

常量mystyle = {显示:“块”,};

const mystyle = {
    display: "block",
  };

  <div className="Works" style={slideIndex == 0 ? mystyle : null}></div>

相关问题