0
我使用下面的代碼來創建與「隨機」產品的div。從PHP收到的JSON數據是這樣的:通過jQuery迭代與.aach ajax json不工作在IE
{"New":[{"product_id":"50",...},...],
"Best":[{"product_id":"26",...},...],
...}
「新」的產品必須去<div id="New">
,「最佳」到「最佳」等。
代碼:
$.ajax({
url: "/index.php?AjaxRequest&action=5",
dataType: "json",
error: function (xhr, status, errorThrown) {
alert(errorThrown + '\n' + status + '\n' + xhr.statusText);
},
success: function (data) {
$.each(data, function (key, value) {
var new_str = '<ul>';
$(value.sort(function() {
return 0.5 - Math.random()
}).slice(0, 3)).each(function() {
new_str += '<li><a href="' + this.link + '" class="right_sidebar">';
new_str += '<img class="right_sidebar_thumb" src="' + this.image + '" alt="' + this.name + '"/></a></li>';
});
new_str += '</ul>';
$('#' + key).append(new_str);
});
}
});
問題出現只能用IE瀏覽器。它只是第一次迭代,只填充第一個div,而其他所有瀏覽器都正常工作。
該問題不是重複的div ids,而JSON是有效的,而jQuery沒有提供任何錯誤。
這樣的隨機函數排序不是一個超好的想法,因爲你真的不知道需要多長時間才能完成它,反正它不會是一個好的洗牌。 – Pointy
哪個。每個不重複,外部還是內部? –
外層只有IE第一次去 –