2014-10-10 33 views
0

我喜歡有一個數組包含10個不同的食譜,在每個頁面上加載我想用這些食譜填充一個div,我是新的數組,但我不知道爲什麼沒有在此提醒?從數組中挑選隨機項並填充 - jQuery

var recipes = [{ 
    "image": "image1.jpg", 
    "ingredients": { 
     "a": "eggs", 
     "b": "chips" 
    }, 
    "link" : "google.com" 
}, { 
    "image": "image2.jpg", 
    "ingredients": { 
     "a": "chocolate", 
     "b": "cheese" 
    }, 
    "link" : "msn.com" 
}]; 

var recipe = recipes[0]; 

alert(recipe); 

http://jsfiddle.net/6otvzr1p/1/

+3

我得到'[對象的對象]'是'alert'的正常輸出當被要求顯示一個JS對象時。 – Alnitak 2014-10-10 09:37:53

+0

這在這裏工作 – devqon 2014-10-10 09:37:54

+0

'食譜[0] .image'是你想在你的HREF? – 2014-10-10 09:38:44

回答

0

其實你只得到正確的值。解析配方對象並放置所需的值。 可以使用此代碼來獲取其可以作爲索引被傳遞到陣列0和9之間的隨機數

Math.floor((Math.random() * 10))