简体   繁体   中英

Image Slider JavaScript

I write this code but next button is not working it shows error Cannot set property 'src' of undefined

<script language="javascript" >
    var pics=new Array("img1.jpeg","img2.jpg","img3.jpg","img4.jpg","img5.jpg");
    var count=0; var len=pics.length;
    function next() {
        // body...
        count++;
        if (count<len) {
            document.pic.src=pics[count];
        }
        else{
            count=0;
            document.pic.src=pics[count];
        }
    }
    function changePic(imgSrc) {
    document.getElementById("pic").src=imgSrc;

}
</script>

When i Click Next button it will show next image from the Slide bar

  1. Do with document.getElementById("pic") instead of document.pic . document.id to target the element is not a good practise

OR use changePic(pics[count])

var pics=new Array("img1.jpeg","img2.jpg","img3.jpg","img4.jpg","img5.jpg");
    var count=0; var len=pics.length;
    function next() {
        // body...
        count++;
        if (count<len) {
            document.getElementById("pic").src=pics[count];
            //OR
            changePic(pics[count]);
        }
        else{
            count=0;
            document.getElementById("pic").src=pics[count];
            //OR
             changePic(pics[count]);
        }
    }
    function changePic(imgSrc) {
      document.getElementById("pic").src=imgSrc;
    }

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