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

我的目标是每个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); }); 

 $(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); }); }); 
 $(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); } }); 

代码应该是这样的……

 $(document).ready(function() { $('.main').each{ Function(){ $(this).css("background-color", hue); }; }; }); 

对不起,对不起。 为了我自己的理智而修复了最糟糕的事情……但是另一个答案让我感到害怕。