gpt4 book ai didi

javascript - 这个错误 "Uncaught TypeError: Cannot read property ' style' of undefined” 是什么意思?

转载 作者:行者123 更新时间:2023-12-02 21:36:41 24 4
gpt4 key购买 nike

我刚刚更改了 img 文件,但它有此消息“未捕获类型错误:无法读取未定义的属性“样式””。我的代码出了什么问题?

<body>

<h2 style="text-align:center">Slideshow Gallery</h2>

<div class="slideshow-container">

<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="COLOURBOX1.jpg" style="width:100%">
<div class="text">Caption One</div>
</div>

<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="COLOURBOX10.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>

<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="COLOURBOX2.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>

</div>

<br>

<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>

var slideIndex = 0;
showSlides();

这是下面的JS。并且“slides[slideIndex-1].style.display = "block";”这部分有我提到的错误消息。

function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var 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";
dots[slideIndex-1].className += " active";
setTimeout(showSlides, 2000); // Change image every 2 seconds
}

最佳答案

看来你的代码在 DOM 完全加载之前就已经运行了。您可以将代码放在 body 标记的底部,也可以使用 DOMContentLoaded 包裹代码。 :

The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.

<script>
document.addEventListener('DOMContentLoaded', (event) => {
var slideIndex = 0;
showSlides();

function showSlides() {
var i;
var slides = document.getElementsByClassName("mySlides");
var 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";
dots[slideIndex-1].className += " active";
setTimeout(showSlides, 2000); // Change image every 2 seconds
}
});
</script>

<h2 style="text-align:center">Slideshow Gallery</h2>
<div class="slideshow-container">
<div class="mySlides fade">
<div class="numbertext">1 / 3</div>
<img src="COLOURBOX1.jpg" style="width:100%">
<div class="text">Caption One</div>
</div>

<div class="mySlides fade">
<div class="numbertext">2 / 3</div>
<img src="COLOURBOX10.jpg" style="width:100%">
<div class="text">Caption Two</div>
</div>

<div class="mySlides fade">
<div class="numbertext">3 / 3</div>
<img src="COLOURBOX2.jpg" style="width:100%">
<div class="text">Caption Three</div>
</div>

</div>

<br>

<div style="text-align:center">
<span class="dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>

关于javascript - 这个错误 "Uncaught TypeError: Cannot read property ' style' of undefined” 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60473499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com