随机排列子元素

Avatar of Chris Coyier
Chris Coyier
$.fn.shuffleChildren = function() {
    $.each(this.get(), function(index, el) {
        var $el = $(el);
        var $find = $el.children();

        $find.sort(function() {
            return 0.5 - Math.random();
        });

        $el.empty();
        $find.appendTo($el);
    });
};

用法

$(".parent-element").shuffleChildren();

查看 CodePen 上的
jQuery 随机排列函数
,作者 Chris Coyier (@chriscoyier)
CodePen 上。