将我附加到另一个的 div 随机化

2022-01-22 00:00:00 random append jquery javascript html

这就是我所拥有的:

$(function(){
    $('.apple').appendTo('.container');
});

现在,假设我有 20 个不同的 div,类名为 .apple.

Now, lets say I have 20 different divs with class name .apple.

我怎样才能让它们Container随机附加到Container中?所以每次我加载页面时,顺序都会不同.

How can I have them append into Container randomly? So every time that I load the page the order would be different.

提前谢谢你!

推荐答案

$.when($(".apples"), [])
    .done(function (data, apples) {
    $.each(data, function (k, v) {
        setTimeout(function () {
            apples.push(v);
            if (apples.length === data.length) {
                $(apples).appendTo(".container")
            };
        }, 1 + Math.floor(Math.random() * 5));
    });
});

jsfiddle http://jsfiddle.net/guest271314/d4p39/

相关文章