简体   繁体   English

Javascript重置循环,直到达到页面上的最大元素数

[英]Javascript reset a loop until max number of elements on page reached

I'm playing around trying to learn more Javascript and I have a json list of images (for now just placeholders) and I'm replacing the img src of all images on the page with the src from the json file. 我在玩耍以尝试了解更多Javascript,并且有图像的json列表(现在仅占位符),并且用json文件中的src替换页面上所有图像的img src

My problem is that when I hit the 4 image sources listed in the json file, the script stops. 我的问题是,当我点击json文件中列出的4个图像源时,脚本停止了。 What I need to do is reset the script to continue from the start again until all images have their src changed... 我需要做的是重置脚本以从头开始再次继续,直到所有图像的src都已更改...

JS: JS:

var fogle_booth = [{
        "name": "Fogle in trunks",
        "image": "http://placehold.it/350x150"
    }, {
        "name": "Fogles does fishing",
        "image": "http://placehold.it/450x150"
    }, {
        "name": "We love fogle",
        "image": "http://placehold.it/550x150"
    }, {
        "name": "Fogle goes walking",
        "image": "http://placehold.it/650x150"
}];


for(var i = 0; i < fogle_booth.length; i++) {

    var obj = fogle_booth[i];
    var image_src = document.getElementsByTagName("img");
    image_src[i].setAttribute("src",(obj.image));
    console.log(obj.image);

    if(image_src.length > obj.length) {
        console.log("resetting");
        continue;
    }
}

I attempted something (which could be miles off) - but I think I may be using the if statement hugely wrong. 我尝试了一些操作(可能相距甚远)-但我认为我可能使用了if语句,这是非常错误的。

Say there is 10 images on the page like: 假设页面上有10张图片,例如:

<img src="myimage.jpg" /> 

I need to be able to reset the loop until all 10 images have their src changed. 我需要能够重置循环,直到所有10张图像的src都更改为止。 Any help will be much appreciated. 任何帮助都感激不尽。

Revised to take into account the length of your JSON and use ternary operator to save space 修改后考虑了JSON的长度并使用三元运算符来节省空间

You want you for loop to run on the number of images, not the number of items in your JSON. 您希望for循环在图像数量上运行,而不是在JSON中的项目数量上运行。 Then, use a separate variable to count through your JSON array and reset when it has hit 3 (ie 4 items total). 然后,使用一个单独的变量对JSON数组进行计数,并在命中3(即总共4个项目)时将其重置。

var img_counter = 0;
for(var i = 0; i < document.images.length; i++) {

    var obj = fogle_booth[img_counter];
    var image_src = document.getElementsByTagName("img");
    image_src[i].setAttribute("src",(obj.image));

    img_counter++;
    img_counter = (img_counter == fogle_booth.length) ? 0 : img_counter;

}

You can view the fiddle here: https://jsfiddle.net/o2cfhf3s/3/ 您可以在这里查看小提琴: https : //jsfiddle.net/o2cfhf3s/3/

// get the list of images first
var imgs = document.getElementsByTagName("img");

// set a counter
var count = 0;

// loop through the image nodelist
for (var i = 0, l = imgs.length; i < l; i++) {

    // use count to grab the object from the array
    var obj = fogle_booth[count];

    // set your image source to be the value of the image name in your object
    imgs[i].src = obj.image;

    // increase the count
    // if it hits the length of the array, reset count
    count++;
    if (count === fogle_booth.length - 1) count = 0;
}

DEMO DEMO

You can also do it this way 你也可以这样

    <script>
var fogle_booth = [{
        "name": "Fogle in trunks",
        "image": "http://placehold.it/350x150"
    }, {
        "name": "Fogles does fishing",
        "image": "http://placehold.it/450x150"
    }, {
        "name": "We love fogle",
        "image": "http://placehold.it/550x150"
    }, {
        "name": "Fogle goes walking",
        "image": "http://placehold.it/650x150"
}];

   var image_src = document.getElementsByTagName("img");
var curr_img=0,count=image_src.length;
for(var i = 0; i < fogle_booth.length; i++) {
    if(curr_img<count){
    var obj = fogle_booth[i];
    image_src[curr_img++].setAttribute("src",(obj.image));
    console.log(obj.image);
    if(i==fogle_booth.length-1)
    i=-1;
    }
}
</script>

 var fogle_booth = [{ "name": "Fogle in trunks", "image": "http://placehold.it/350x150" }, { "name": "Fogles does fishing", "image": "http://placehold.it/450x150" }, { "name": "We love fogle", "image": "http://placehold.it/550x150" }, { "name": "Fogle goes walking", "image": "http://placehold.it/650x150" }]; var images = document.getElementsByTagName("img").length; while (images > 0) { var rest = fogle_booth.length > images ? images : fogle_booth.length; for(var i = 0; i < rest; i++) { var obj = fogle_booth[i]; var image_src = document.getElementsByTagName("img"); image_src[images-1].setAttribute("src",(obj.image)); console.log(obj.image); images--; } } 
 <img></img> <img></img> <img></img> <img></img> <img></img> <img></img> <img></img> <img></img> <img></img> <img></img> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM