Trộn các phần tử DOM - Thủ thuật CSS

Anonim

Đây là từ James Padolsey. Hãy xem bài viết của anh ấy để biết kỹ thuật JavaScript thuần túy.

Cắm vào

(function($)( $.fn.shuffle = function() ( var allElems = this.get(), getRandom = function(max) ( return Math.floor(Math.random() * max); ), shuffled = $.map(allElems, function()( var random = getRandom(allElems.length), randEl = $(allElems(random)).clone(true)(0); allElems.splice(random, 1); return randEl; )); this.each(function(i)( $(this).replaceWith($(shuffled(i))); )); return $(shuffled); ); ))(jQuery);

Sử dụng

Mục tiêu các yếu tố, xáo trộn cuộc gọi.

$('ul#list li').shuffle();