它说无法在我的代码中的函数中读取null的属性“ children”,请帮助我,我在这里停留的时间太长了。
我尝试重命名ID并检查拼写错误的代码。
<div id="carousel-slide">
<img src="image1.jpg">
<img src="image2.jpg">
<img src="image3.jpg">
</div>
<script type="text/javascript">
var carousel_index = 0;
function show_slides(){
var carousel_image = document.getElementById("carousel-slide").children;
var max = carousel_image.length - 1;
carousel_index+=1;
if(carousel_index>max) carousel_index = 0;
if(carousel_index<0) carousel_index = max;
for(var i=0; i<=max; i++){
carousel_image[i].className = "carousel-hidden";
}
carousel_image[carousel_index].className = "carousel-show";
setTimeout(show_slides,4000);
}
show_slides();
</script>
答案 0 :(得分:0)
我猜该元素尚未加载。试试这个:
document.addEventListener("DOMContentLoaded", function() {
show_slides();
});