文章“随机排序子元素”的直接链接

随机排序子元素

$.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

Avatar of Chris Coyier
Chris Coyier 发布