我有this code使用getJSON獲取隨機文本鏈接。它一次拾取一個隨機項目。我想知道如何提取多個不同的物品並在#randomkeyword
中顯示。我所能想到的是設置劇本兩次,但我認爲它可能有機會拿起相同的項目,有沒有辦法做到這一點?如何在此javascript中獲取多個隨機不同項目
jQuery.getJSON('random.json', function(data) { //Ajax call
var item = data.link[Math.floor(Math.random()*data.link.length)];
jQuery('<a title="' + item.des + '" href="http://' + item.url + '">'+ item.title +'</a>').appendTo
('#randomkeyword');
});
JSON文件:
{"link":[{"title":"XXXX","url":"google.com","des":"light"},{"title":"CCCCCCC","url":"yahoo.com","des":"dark"},{"title":"DDDDDDDD","url":"song.com","des":"light"},{"title":"CCCCCCCCCCCCCCC","url":"googlemap.com","des":"normal"},{"title":"RRRRRRRRRRRRRRR","url":"fun.com","des":"halo"}]}
http://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array-in-javascript –