带有Array的Javascript图库

时间:2013-09-29 17:42:04

标签: javascript html image-gallery

我正在尝试使用在点击时触发javascript的锚来修改html img源代码,但它无效。这是我的代码:

HTML

<body>
<img src='./images/img1.png' id='gallery'>
<a href="#" id="back">Back</a> - <a href="#" id="next">Next</a>
</body>

JS

var image = new Array(3)
image[1]="./images/img1.png"
image[2]="./images/img2.png"
image[3]="./images/img3.png"
var num = 1;

document.getElementById('next').addEventListener('click', function(galleryNext){
num=num+1
if (num==4)
{num=1}
document.getElementById('gallery').src=image[num];
});

document.getElementById('back').addEventListener('click', function(galleryPrevious){
num=num-1
if (num==0)
{num=3}
document.getElementById('gallery').src=image[num];
});

1 个答案:

答案 0 :(得分:0)

好吧,把js代码放在一个函数中并在body onload事件上调用它