你可以试试这个,使用jquery
,
function GenerateColor() {
var color = '#' + Math.floor(Math.random() * 16777215).toString(16);
return color;
}
$(document).ready(function(e) {
$("#ColorTable").css('backgroundColor', GenerateColor());
});
HTML:
<table>
<tr id="ColorTable">
<th>Item Name</th>
<th>Quantity</th>
</tr>
</table>
简单的随机类方法:
var cssArray = new Array('Class1', 'Class2', 'Class3');
$(document).ready(function(e) {
var i = Math.floor((Math.random()*3));
$("#ColorTable").addClass(cssArray[i]);
});
CSS:
.Class1{
background-color:blue;
}
.Class2{
background-color:red;
}
.Class3{
background-color:green;
}
应用于每一行的背景颜色:
$(document).ready(function(e) {
$("#ColorTable tr").each(function(){
$(this).css('backgroundColor', GenerateColor());
});
});