基于@anu 在较早的 stackoverflow 答案中指出的示例,这是 jsbin 中的一个实时示例。
http://jsbin.com/ehodul/4/edit#javascript,html,live
下面是(大部分)代码,但我再次鼓励您修改 jsbin 上的示例。
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="jquery.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
table { padding: 0; border-collapse: collapse; }
table th { background-color: #eee; }
table td, th { border: 1px solid #d7d7d7; padding: 7px; }
</style>
</head>
<body>
<select id="myOptions">
<option value="1">hide col 1</option>
<option value="2">hide col 2</option>
<option value="3">hide col 3</option>
<option value="4">hide col 4</option>
</select>
<p> </p>
<table id="myTable" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th>col 1</th>
<th>col 2</th>
<th>col 3</th>
<th>col 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>1abcdefg</td>
<td>2abcdefg</td>
<td>3abcdefg</td>
<td>4abcdefg</td>
</tr>
<tr>
<td>1abcdefg</td>
<td>2abcdefg</td>
<td>3abcdefg</td>
<td>4abcdefg</td>
</tr>
<tr>
<td>1abcdefg</td>
<td>2abcdefg</td>
<td>3abcdefg</td>
<td>4abcdefg</td>
</tr>
<tr>
<td>1abcdefg</td>
<td>2abcdefg</td>
<td>3abcdefg</td>
<td>4abcdefg</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
function hideColumn(columnIndex) {
$('#myTable thead th, #mytable tbody td').show();
$('#mytable thead th:nth-child('+(columnIndex)+'), #mytable tbody td:nth-child('+(columnIndex)+')').hide();
}
$("#myOptions").change(function() {
selectedVal = $(this).val();
console.log(selectedVal);
hideColumn( selectedVal );
});
</script>
</body>
</html>