简体   繁体   中英

How to change the URL of an image tag on every click on the image using JavaScript?

I have an element displaying an image on an HTML page. This element's source is one of many different images in a JavaScript array. I already have a script for looping through the images, creating a slideshow effect, but now I want to manually flick through the images with buttons.

This is my code so far, but I get no response when clicking the button.

function nextup() 
{
    imgs = []; 
    imgs[0] = "/snakelane/assets/images/thumb/_1.jpg";  imgs[10] = "/snakelane/assets/images/thumb/_19.jpg"; 
    imgs[1] = "/snakelane/assets/images/thumb/_2.jpg";  imgs[11] = "/snakelane/assets/images/thumb/_20.jpg"; 
    imgs[2] = "/snakelane/assets/images/thumb/_3.jpg";  imgs[12] = "/snakelane/assets/images/thumb/_21.jpg";
    imgs[3] = "/snakelane/assets/images/thumb/_4.jpg";  imgs[13] = "/snakelane/assets/images/thumb/_22.jpg";
    imgs[4] = "/snakelane/assets/images/thumb/_5.jpg";  imgs[14] = "/snakelane/assets/images/thumb/_23.jpg";    
    imgs[5] = "/snakelane/assets/images/thumb/_6.jpg";  imgs[15] = "/snakelane/assets/images/thumb/_24.jpg";
    imgs[6] = "/snakelane/assets/images/thumb/_7.jpg";  imgs[16] = "/snakelane/assets/images/thumb/_25.jpg";
    imgs[7] = "/snakelane/assets/images/thumb/_8.jpg";  imgs[17] = "/snakelane/assets/images/thumb/_26.jpg"; 
    imgs[8] = "/snakelane/assets/images/thumb/_9.jpg";  imgs[18] = "/snakelane/assets/images/thumb/_27.jpg";
    imgs[9] = "/snakelane/assets/images/thumb/_32.jpg"; imgs[19] = "/snakelane/assets/images/thumb/_28.jpg"; 

    var pic = document.getElementById("picbox");

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

        var current = indexOf(pic.src);
        var next = Math.round(current + 1);
        pic.src = imgs[next];
    }
}

Can anyone tell me what's wrong with my code or suggest a better way?

Multiple problems in the approach you had used. Have a look at the modified function below. Let me know if you need explanation with anything.

The following code will use an array containing image URLs and later assign in a sequential manner to an img tag on click. Enjoy!

Here you can try to see the output.

function nextup(){

 //Initialized img array with 10 images, you can do it any way you want to.

 var imgs = []; 
 for(i=0;i<10;i++){
      imgs[i] = "http://lorempixel.com/output/cats-q-c-100-100-"+(i+1)+".jpg";
 }    

 //Fetch the pic DOM element by ID

var pic = document.getElementById("picbox");

//Know what is position of currently assigned image in array.

 var current = imgs.indexOf(pic.src);
 var next = 0; 
 //Handle case if no image is present, the initial case.
 if(current!=-1){
  next = (current + 1)%(imgs.length);
 }

 //Assign the next src
 pic.src = imgs[next];

}
//Scoped outside to call the function first time on load.

nextup();

I found the following problems in your code:

  1. You tried to use indexOf without specifying the array in which the search has to be performed. Imagine s school principal asking someone to go find if John is present in the classroom without specifying a specific classroom.
  2. For iterating through array you used a next variable which could have been a good idea if you needed an endless loop. But here since we are limited to 10 or 20 images we need to make sure that if the currently selected image is the last one, we find that next goes to 21 (assuming a total of 20 images.) and this would try to access a variable out of bounds.

Hence I've used the mod operator % . For reference in JavaScript, 5%10 would return 5 , 15%10 would return 5 and so on. Read more about the mod operator HERE .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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