<html>
<head>
<title></title>
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function addCol() {
$th = $("<th>增加的列头</th>");
$col = $("<td>增加的列</td>");
$("#tab1>thead>tr").append($th);
$("#tab1>tbody>tr").append($col);
}
function delCol() {
//移除最后一列
$("#tab1 tr :last-child").remove();
//移除第一列
//$("#tab1 tr :first-child").remove();
//移除指定的列, 注:这种索引从1开始
//$("#tab1 tr :nth-child(2)").remove();
//移除第一列之外的列
//$("#tab1 tr :not(:nth-child(1))").remove();
}

</script>
</head>
<body>
<input id="Button1" type="button" onclick="addCol()" value="增加列" />
<input id="Button2" type="button" onclick="delCol()" value="减少列" />
<table id="tab1" border="1" style="width: 200px;">
<thead>
<tr>
<th>
&nbsp;
Id</th>
<th>
&nbsp;
Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>
&nbsp;
1</td>
<td>
&nbsp;
叶子</td>
</tr>
<tr>
<td>
&nbsp;
2</td>
<td>
&nbsp;
王子</td>
</tr>
</tbody>
</table>
</body>
</html>

posted on 2016-03-18 10:33  来临  阅读(254)  评论(0编辑  收藏  举报