<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var colspanNum=0;
window.onload=function(){
var thisTable = document.getElementById("thisTable");
var trs = thisTable.getElementsByTagName("tr");
for(var i=1;i<trs.length;i++){
var tds = trs[i].getElementsByTagName("td");
if(i==1){
var tdLength = tds.length;
colspanNum = tdLength;
var spanButton = document.getElementById("spanButton");
spanButton.colspan=colspanNum;
console.log(colspanNum);
}
for(var j=0;j<tds.length;j++){
if(j==1){
console.log(tds[j].innerHTML);
}
}
}
}
//console.log(colspanNum);
</script>
</head>
<body>
<table id="thisTable">
<tr>
<td>id</td>
<td>name</td>
<td>age</td>
</tr>
<tr>
<td>001</td>
<td>Laowang</td>
<td>10</td>
</tr>
<tr>
<td>002</td>
<td>Yan</td>
<td>9</td>
</tr>
<tr>
<td id="spanButton"><button>button</button></td>
</tr>
</table>
</body>
</html>
1.spanButton.colSpan=colspanNum;這一行S要大寫
2.設置之後單元格列寬實際已經生效,只是button的寬度未變而已,不信可以在table加上border='1'看看效果