getJSON を使用してランダムなテキスト リンクを取得するこのコードがあります。一度に 1 つのランダムなアイテムをピックアップします。複数の異なるアイテムをピックアップして に表示する方法を知りたい#randomkeyword
です。思いついたのはスクリプトを 2 回セットアップすることだけですが、同じアイテムをピックアップする可能性があると思います。それを行う方法はありますか?
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"}]}