Dựa không @Alexey Lebedev của câu trả lời, nếu bạn thích một hàm jQuery mà shuffles yếu tố, bạn có thể sử dụng một:
$.fn.randomize = function(selector){
var $elems = selector ? $(this).find(selector) : $(this).children();
for (var i = $elems.length; i >= 0; i--) {
$(this).append($elems[Math.random() * i | 0]);
}
return this;
}
Và sau đó gọi nó như thế này:
$("ul").randomize(); //shuffle all the ul children
$("ul").randomize(".item"); //shuffle all the .item elements inside the ul
$(".my-list").randomize(".my-element"); //shuffle all the .my-element elements inside the .my-list element.
Nguồn
2016-09-14 14:06:17
thể trùng lặp của [jquery yếu tố di chuyển thành một thứ tự ngẫu nhiên] (http://stackoverflow.com/questions/5329201/jquery-move-elements-into-a-random-order) –
@Pekka Dường như anh ấy đang sử dụng jQuery. –