如何在刷新时更改图像,但是该图像仍有链接?这是我尝试过的:
<SCRIPT LANGUAGE="JavaScript">
var theImages = new Array()
//Random-loading images
theImages[0] = 'image' // replace with names of images
theImages[1] = 'image' // replace with names of images
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
preBuffer[i] = new Image()
preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
if(whichImage==0){
document.write('<a href ="link"><img src="'+theImages[0]+'" border=1 width=728 height=90></a>');
}
else if(whichImage==1){
document.write('<a href ="link"><img src="'+theImages[whichImage]+'" border=0 width=728 height=90></a>');
}
}
</script>
<script>showImage();</script>
我在网上找到了更少的代码,但是一切对我来说都不起作用:(有人可以修复此代码或以其他方式帮助我吗?)
答案 0 :(得分:1)
这将在每次运行时随机选择一个图像并相应地设置html img的src属性。
var images = [
'image_1.jpg',
'image_2.jpg',
'image_3.jpg',
'image_4.jpg',
'image_5.jpg'
]
var which = Math.floor(Math.random() * images.length);
var img = document.getElementById('theimage');
img.src = images[which];
img.title = images[which];
&#13;
<img id="theimage" />
&#13;