[英]Table appear when button is clicked
It is possible on my web page when one of the buttons in a menu is clicked, a table will appear and when another button is clicked it will replace the table into another table with different data?当单击菜单中的一个按钮时,我的 web 页面上可能会出现一个表格,当单击另一个按钮时,它会将表格替换为具有不同数据的另一个表格?
I have tried different possibilities, somehow it works but it doesn't replace the table.我尝试了不同的可能性,它以某种方式起作用,但它并不能取代桌子。
document.getElementById("table1").style.display="none";
document.getElementById("btn1").addEventListener("click", function(){ document.getElementById("table1").style.display = "block"; document.getElementById("table2").style.display = "none"; }); document.getElementById("btn2").addEventListener("click", function(){ document.getElementById("table2").style.display = "block"; document.getElementById("table1").style.display = "none"; });
<button id="btn1">button 1</button> <button id="btn2">button 2</button> <table id="table1" style="width:100%;display:none"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td>50</td> </tr> </table> <table id="table2" style="width:100%;display:none"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td>94</td> </tr> </table>
a sample for your question is the code below:您的问题的示例是以下代码:
<!DOCTYPE html>
<html>
<head>
<title>Appear and Disappear</title>
</head>
<body>
<nav>
<button onclick="showTable1()">showTable1</button>
<button onclick="showTable2()">showTable2</button>
</nav>
<table id='table1' style="display:none">
<tr>
<th>Table Name</th>
</tr>
<tr>
<td>
It's table1
</td>
</tr>
</table>
<table id='table2' style="display:none">
<tr>
<th>Table Name</th>
</tr>
<tr>
<td>
It's table2
</td>
</tr>
</table>
<script>
function showTable1() {
document.getElementById("table1").style.display = "block";
document.getElementById("table2").style.display = "none";
} function showTable2() {
document.getElementById("table1").style.display = "none"; document.getElementById("table2").style.display = "block";
}
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.