我目前正在尝试随机附加谷歌电子表格中的JSON数据。有帮助吗?目前,我成功地将数据附加到div,但它们都按顺序出现。我如何随机化它?感谢
$(document).ready(function() {
$(function listBooks() {
$.getJSON("https://spreadsheets.google.com/feeds/list/0Am2EHR2fZWeDdDZmTXhZUkJYZ09xNDhHeXM5Z0ZwQnc/od6/public/values?alt=json-in-script&callback=?", function (data) {
$('div#book-list').append('<ul class="items"></ul>');
$.each(data.feed.entry, function(i,entry) {
var item = '<span style="display:none">' + entry.id.$t + '</span>';
item += '<a href="' + entry.gsx$link1.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
item += '<a href="' + entry.gsx$link2.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
item += '<a href="' + entry.gsx$link3.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
item += '<a href="' + entry.gsx$link4.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
$('.items').append('<li>' + item + '</span></li>');
});
});
});
});
答案 0 :(得分:0)
你可以试试这个
$(document).ready(function() {
$(function listBooks() {
$.getJSON("https://spreadsheets.google.com/feeds/list/0Am2EHR2fZWeDdDZmTXhZUkJYZ09xNDhHeXM5Z0ZwQnc/od6/public/values?alt=json-in-script&callback=?", function (data) {
$('div#book-list').append('<ul class="items"></ul>');
// temporary ul
$ul = $('<ul></ul>');
$.each(data.feed.entry, function(i,entry) {
var item = '<span style="display:none">' + entry.id.$t + '</span>';
item += '<a href="' + entry.gsx$link1.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
item += '<a href="' + entry.gsx$link2.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
item += '<a href="' + entry.gsx$link3.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
item += '<a href="' + entry.gsx$link4.$t + '" onclick="enablesubmit();" target="_blank">Click</a> ';
$ul.append('<li>' + i + '-'+ item + '</span></li>');
});
// Shuffle children
var children = $ul.children();
children = shuffle (children)
// Append children
$('.items').append(children);
});
});
});
function shuffle(o){ //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};