[英]for loop won't enter, not sure why
I am trying to make a moving image gallery where every 4 seconds the image scrolls across. 我正在尝试制作一个移动的图片库,其中每4秒钟滚动一次图像。
I link to the javascript correctly, I'm sure of it. 我确定可以正确链接到javascript。
Here is the javascript, I would like to know what's wrong with it. 这是JavaScript,我想知道它有什么问题。
var slideIndex = 0;
image();
function image() {
alert("Entered Function");
var i;
var x = document.getElementsByClassName("slideshow");
for (i = 0; i < x.length; i++) {
x[i].style.display = none;
alert("Entered Loop");
}
slideIndex++;
if (slideIndex > x.length) {
slideIndex = 1;
}
x[slideIndex-1].style.display = "block";
setTimeout(image, 4000);
}
Any ideas? 有任何想法吗? Thanks
谢谢
EDIT: The missing quotes around style.display = none is not the issue. 编辑:style.display = none周围缺少引号不是问题。 I just forgot to add them back after testing the code.
我只是忘了在测试代码后将它们添加回去。
The reason is being, when the script is executed, the elements aren't present. 原因是,在执行脚本时,元素不存在。 I believe, you are executing the scripts way before the elements are loaded.
我相信,您是在元素加载之前执行脚本的。 There are two ways to handle this:
有两种方法可以解决此问题:
onload
eventListener. onload
eventListener。 And moreover, there's an error in the code, if you check the console. 此外,如果您检查控制台,则代码中会出现错误。 The
= none;
= none;
is not right. 是不正确的。 It should be a string.
应该是一个字符串。 Change the line to:
将行更改为:
x[i].style.display = "none";
A snippet here demonstrates that it works if you load the script after the elements are loaded. 此处的代码段演示了如果在加载元素之后加载脚本,则该片段有效。
<div class="slideshow">Slide 1</div> <div class="slideshow">Slide 2</div> <div class="slideshow">Slide 3</div> <div class="slideshow">Slide 4</div> <div class="slideshow">Slide 5</div> <script> var slideIndex = 0; image(); function image() { alert("Entered Function"); var i; var x = document.getElementsByClassName("slideshow"); for (i = 0; i < x.length; i++) { // Error here: x[i].style.display = "none"; alert("Entered Loop"); } slideIndex++; if (slideIndex > x.length) { slideIndex = 1; } x[slideIndex - 1].style.display = "block"; setTimeout(image, 4000); } </script>
You can see that the above code is working as expected. 您可以看到上面的代码按预期工作。
Your problem is when you set display property: 您的问题是在设置显示属性时:
Change this x[i].style.display = none;
更改此
x[i].style.display = none;
to x[i].style.display = 'none';
到
x[i].style.display = 'none';
, otherwise you will get an error like ReferenceError: none is not defined
, which prevents alert from firing: ,否则会收到类似
ReferenceError: none is not defined
的错误ReferenceError: none is not defined
,这会阻止警报触发:
var slideIndex = 0; image(); function image() { alert("Entered Function"); var i; var x = document.getElementsByClassName("slideshow"); for (i = 0; i < x.length; i++) { x[i].style.display = 'none'; alert("Entered Loop"); } slideIndex++; if (slideIndex > x.length) { slideIndex = 1; } x[slideIndex-1].style.display = "block"; setTimeout(image, 4000); }
<div class='slideshow'></div> <div class='slideshow'></div> <div class='slideshow'></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.