jQuery, random div order

前端 未结 3 2074
别跟我提以往
别跟我提以往 2021-02-04 08:57

I have this jQuery and HTML http://jsfiddle.net/UgX3u/30/

    
3条回答
  •  迷失自我
    2021-02-04 09:31

    This example assumes you have to work with elements already on a page with classes assigned to them:

    var classes = [];
    
    // Populate classes array
    // e.g., ["yellow", "red", "green", "blue", "pink", "orange", "black", "white"]
    $('div.container').children().each(function (i, v) {
        classes.push(v.className);
    });
    
    // Assign random color to each div
    $('div.container').children().each(function (i, v) {
        var color = Math.floor(Math.random()*classes.length)
        $(v).css({backgroundColor: classes.splice(color,1)});
    });
    

    http://jsfiddle.net/UgX3u/40/

提交回复
热议问题