基本上我要做的就是这个 var arr = ["red","green","blue"];$('.box').each(function() { $(this).addClass(Array Value Here)}); 我希望结果是这样的. div class"box red"/divdiv class"box green"/divdiv class"box blue"/divdiv class
var arr = ["red","green","blue"]; $('.box').each(function() { $(this).addClass(Array Value Here) });
我希望结果是这样的.
<div class"box red"></div> <div class"box green"></div> <div class"box blue"></div> <div class"box red"></div> <div class"box green"></div> <div class"box blue"></div> <div class"box red"></div> <div class"box green"></div> <div class"box blue"></div>
我怎样才能做到这一点?
总div的数量未知.
var colours = ['red', 'green', 'blue']; $('.box').each(function(index, element) { $(element).addClass(colours[index % colours.length]); });
See fiddle.