用JS实现随机显示N张图片

JavaScript022

用JS实现随机显示N张图片,第1张

<script type="text/javascript">

function existDuo(a, t){

for(var i in a)

if(a[i]==t)

return true

return false

}

var a=[]

while(a.length<6){

var t=Math.floor(Math.random()*50+1)

if(!existDuo(a,t)) a.push(t)

}

for(var i in a)

document.write('<li><img src="'+a[i]+'.jpg"></li>\n')

</script>

id=(1000+parseInt(Math.random()*500)*1).toString().substr(1)

document.write("<a href='" + id +".html'><img src='./PicShow/" + id + ".gif'></a>")

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />

<title>无标题文档</title>

</head><script language="JavaScript">

today=new Date()

jran=today.getTime()

function rnd() {

ia=9301

ic=49297

im=233280

jran = (jran*ia+ic) % im

return jran/(im*1.0)

}

function rand(number) {

return Math.ceil(rnd()*number)

}

document.write("<center>")

for(i=1i<=1i++) {

myNum=(rand(3))//改成你要随机显示的图片数;

if(myNum == 1) document.write("<img src='1.jpg'>")//图片路径,可用http://

if(myNum == 2) document.write("<img src='2.jpg'>")

if(myNum == 3) document.write("<img src='3.jpg'>")

else {

document.write()

}

}

document.write("</center>")

</script>

<body>

</body>

</html>