如何在浏览器中显示随机图像?

问题描述:

我想显示与JS随机图像,我的代码如下如何在浏览器中显示随机图像?

var imageList=new Array(); 
imageList[0]="img1.jpg"; 
imageList[1]="img2.jpg"; 
imageList[2]="img3.jpg"; 
imageList[3]="img4.jpg"; 
var imageChoice=Math.floor(Math.random*imageList.length); 
document.write('<img src="'+imageList[imageChoice]+'"' ); 
+1

,什么是错? – 2014-10-12 09:05:40

random()Math object的方法。你错过了支架,以将其作为调用一个函数:

var imageChoice = Math.floor(Math.random() * imageList.length); 

作为一个方面说明,我建议你初始化图像阵列using square bracket notation这样的:

var imageList = ["img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg"]; 

下面是一个样本选择随机颜色来自一行代码!

[].forEach.call($$("*"),function(a){ 
 
    a.style.outline="1px solid #"+(~~(Math.random()*(1<<24))).toString(16) 
 
})

check the detail from this link