2014-10-12 55 views
0

我想顯示與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

回答

0

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"]; 
0

下面是一個樣本選擇隨機顏色來自一行代碼!

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

check the detail from this link