2014-10-10 32 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))