将随机颜色单独应用于类元素?

3 javascript css jquery

我的目标是每个div都有一个"main"类,有一个随机的背景颜色.我有生成随机颜色的脚本,但是,使用jquery,我似乎只能将它应用于类中的所有div.如何选择div,应用颜色,选择类中的下一个div,生成新的随机颜色,应用它并重复?这是我的代码:

$(document).ready(function() {
    var hue = 'rgb(' + (Math.floor((256-199)*Math.random()) + 200) + ','
                     + (Math.floor((256-199)*Math.random()) + 200) + ','
                     + (Math.floor((256-199)*Math.random()) + 200) + ')';
    $('.main').css("background-color", hue);
});
Run Code Online (Sandbox Code Playgroud)

小智 20

$(document).ready(function() {
    $('.main').each(function () {
        var hue = 'rgb(' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ',' + (Math.floor((256-199)*Math.random()) + 200) + ')';
        $(this).css("background-color", hue);
    });
});
Run Code Online (Sandbox Code Playgroud)