gpt4 book ai didi

javascript - 刷新时更改图像

转载 作者:行者123 更新时间:2023-12-03 10:04:40 26 4
gpt4 key购买 nike

如何在刷新时更改图像,但该图像仍有链接?这是我尝试过的:

<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>

我在网上发现了一些代码,但一切都不适合我:(有人可以修复此代码或以其他方式帮助我吗?:)

最佳答案

这将在每次运行时随机选择一个图像,并相应地设置 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];
<img id="theimage" />

关于javascript - 刷新时更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com