0

我正在尝试使用在单击时触发 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];
});
4

1 回答 1

0

好吧,将js代码放在一个函数中,并在onloadbody的事件中调用它

于 2013-09-29T17:45:39.977 回答