html中table设置每个单元格的不同颜色和宽度
展开全部
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
RunJS 演示代码
</title>
<style>
table{
margin:auto;
width:50%;
border:1px solid black;
border-collapse:collapse;
}
td{
text-align:center;
border:1px solid black;
}
</style>
<script>
onload=function(){
var table = document.querySelector("table");
var rows = table.tBodies[0].rows;
for(var i = 0; i < rows.length; i++){
var tds = rows[i].cells;
for(var j = 0; j < tds.length; j++){
var td = tds[j];
td.style.width = Math.random()*1000 + "px";
td.style.backgroundColor = "rgb(" + Math.floor(Math.random()*255) + "," + Math.floor(Math.random()*255) + "," + Math.floor(Math.random()*255) + ")";
}
}
}
</script>
</head>
<body>
<table>
<tr>
<td>
11
</td>
<td>
22
</td>
<td>
22
</td>
<td>
22
</td>
<td>
22
</td>
<td>
22
</td>
</tr>
<tr>
<td>
11
</td>
<td>
22
</td>
<td>
22
</td>
<td>
22
</td>
<td>
22
</td>
<td>
22
</td>
</tr>
</table>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询