3 回答
![?](http://img1.sycdn.imooc.com/5333a0aa000121d702000200-100-100.jpg)
TA贡献1796条经验 获得超4个赞
使用单个选择。并给你想要隐藏的表隐藏的属性并使用下面给出的javascript
function myFunction() {
var tables = document.getElementsByClassName("toggletable");
var targetval = document.getElementById("select").value;
for(var i = 0; i < tables.length; i++){
if(targetval == i + 1){
tables[i].removeAttribute("hidden");
}else{
tables[i].setAttribute("hidden", true);
}
}
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#Tables {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: blue;
margin-top: 20px;
}
#Tables1 {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: Purple;
margin-top: 20px;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
</head>
<body>
<select id="select" onchange="myFunction()">
<option value="1" >Table 1</option>
<option value="2">Table 2</option>
<option value="3">Table 3</option>
<option value="4">Table 4</option>
<option value="5">Table 5</option>
</select>
<div id="Tables" class="toggletable">
<table>
<tr>
<th>Cost</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td>Price1</td>
<td>Names1</td>
<td>Place1</td>
</tr>
</table>
</div>
<div id="Tables1" class="toggletable" hidden>
<table>
<tr>
<th>Cost</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td>Price2</td>
<td>Names2</td>
<td>Place2</td>
</tr>
</table>
</div>
<div class="toggletable" hidden>
<table>
<tr>
<th>Cost</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td>Price3</td>
<td>Names3</td>
<td>Place3</td>
</tr>
</table>
</div>
<div class="toggletable" hidden>
<table>
<tr>
<th>Cost</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td>Price4</td>
<td>Names4</td>
<td>Place4</td>
</tr>
</table>
</div>
<div class="toggletable" hidden>
<table>
<tr>
<th>Cost</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td>Price5</td>
<td>Names5</td>
<td>Place5</td>
</tr>
</table>
</div>
<script>
function myFunction() {
var tables = document.getElementsByClassName("toggletable");
var targetval = document.getElementById("select").value;
for(var i = 0; i < tables.length; i++){
if(targetval == i + 1){
tables[i].removeAttribute("hidden");
}else{
tables[i].setAttribute("hidden", true);
}
}
}
</script>
</body>
</html>
添加回答
举报