根据不存在的元素断开循环 [英] Break loop based on element not existing

查看:158
本文介绍了根据不存在的元素断开循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个cms,允许用户在幻灯片中添加多达10个图像,这些图像都在前端以div的形式输出,并带有showcaseSlide,末尾添加0-9的数字。 showcaseSlide0,showcaseSlide1等。对于控制幻灯片显示的javascript,我需要将所有的div id输出到数组中,但在幻灯片播放结束时结束数组,例如,如果div id从showcaseSlide0 - showcaseSlide3中去掉了,我需要数组从幻灯片[0] - 幻灯片[3]。

下面是当前代码和一些我之前尝试过的注释过的代码:

  var slides = new Array(); 
var count = 0;
for(i = 0; i <= 10; i ++){
slides [i] =showcaseSlide+ i;
document.write(幻灯片[i]); //这样我就可以看到数组中的哪个id
var div = document.getElementById(slides [i]);
// if(div){break; }< - 不会中断
//if(文档.getElementById (slides[i])== null)break; <在第一个
之后的破坏//if(文档.getElementById(slides [i])==未定义)break; < - 在第一个
之后休息;

编辑:

我已经发现(感谢Teemu下面评论)它没有工作,因为它在页面加载之前被调用,因此在对象被渲染之前被调用。我还要感谢Peter Kelly(他也在下面发表了评论),他指出我需要使用!在我的break语句中,Frazer指出我的循环太大了。



以下是新代码(包括初始化函数的其他元素): p>

  var count = 0; 
var wait = 4000;
var slides = [];

函数startShowcase(){

for(var i = 0; i <10; i ++){
slides [i] =showcaseSlide+ i; ;
if(!document.getElementById(slides [i])){break; }
};
setInterval(showcase,wait);

};


解决方案

您的DIV编号为0-9,但循环运行11次。



不是实际的代码,但是这可以解释它。

  for(i = 0; i <= 10; i ++){
0 = 1st
1 = 2nd
2 = 3rd
3 = 4th
4 = 5th
5 =第6个
6 =第7个
7 =第8个
8 =第9个
9 =第10个
10 =第11个
}


I have built a cms that allows users to add up to 10 images into the slideshow, which all output in the front end in divs with ids of showcaseSlide with a number from 0-9 appended on the end, e.g. showcaseSlide0, showcaseSlide1 etc. For the javascript that controls the slideshow, I need to output all of the div id's into an array, but end the array when the slides finish, eg if the div ids went from showcaseSlide0 - showcaseSlide3, I would need the array to go from slides[0] - slides[3].

Here is the current code and some commented out code that I have tried before:

var slides = new Array();
var count = 0;
for(i=0; i<=10; i++){
slides[i] = "showcaseSlide"+i;
document.write(slides[i]); //so that I can see which id's are in the array
var div = document.getElementById(slides[i]);
//if(div) { break; } <- doesn't break
//if(document.getElementById(slides[i]) == null) break; <-breaks after 1st
//if(document.getElementById(slides[i]) == undefined) break; <- breaks after 1st
};

Edit:

I've found out (thanks to Teemu who commented below) that it wasn't working because it was called before the page load, therefore before the objects were rendered. I also have to thank Peter Kelly (who also commented below), who pointed out that I needed to use a ! in my breaking if statement and Frazer who pointed out my loop was 1 too big.

Here is the new code (including the other elements of the initialising function):

var count = 0;
var wait = 4000;
var slides = [];

function startShowcase() {

    for(var i=0; i<10; i++){
        slides[i] = "showcaseSlide"+i;;
       if(!document.getElementById(slides[i])) { break; }
    };
    setInterval(showcase, wait);

};

解决方案

You have DIVs numbered 0-9 but your loop runs 11 times.

Not actual code, but this explains it.

for(i=0; i<=10; i++){
  0 = 1st 
  1 = 2nd
  2 = 3rd
  3 = 4th
  4 = 5th
  5 = 6th
  6 = 7th
  7 = 8th
  8 = 9th
  9 = 10th
  10 = 11th
}

这篇关于根据不存在的元素断开循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆