所以基本上我正在寻找的是如何拥有一个随机图像 javascript 代码,但图像位于两个不同的 div 中,但我希望随机图像来自同一个数组。
我计划今年夏天上JS课,所以我不必再问了,因为我觉得这应该很简单......
目前我只是在两个不同的位置使用来自 javascript kit 的代码:
<script language="JavaScript">
<!--
/*
Random Image Script- By JavaScript Kit (http://www.javascriptkit.com)
Over 400+ free JavaScripts here!
Keep this notice intact please
*/
function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="image1.gif"
myimages[2]="image2.gif"
myimages[3]="image3.gif"
myimages[4]="image4.gif"
myimages[5]="image5.gif"
myimages[6]="image6.gif"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)ry=1
document.write('<img src="'+myimages[ry]+'" border=0>')
}
random_imglink()
//-->
</script>
但我希望实现的是:
<script language="JavaScript">
<!--
/*
Random Image Script- By JavaScript Kit (http://www.javascriptkit.com)
Over 400+ free JavaScripts here!
Keep this notice intact please
*/
function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="image1.gif"
myimages2[1]="image1a.gif"
myimages[2]="image2.gif"
myimages2[2]="image2a.gif"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0) ry=1
document.write('<img src="'+myimages[ry]+'" border=0>')
}
random_imglink()
//-->
</script>
DIVS 中的渲染代码
<div class="one"><img src="img1.gif"></div>
<div class="two"><img src="img1a.gif"></div>
刷新
<div class="one"><img src="img2.gif"></div>
<div class="two"><img src="img2a.gif"></div>