How to hide a table in javascript: first create an HTML sample file; then create a table in the body; finally implement it through the js code "getElementById("table1").style.display='block';" Just hide it.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
Javascript displays and hides a table
The document.getElementById("Element ID") method and the style.display attribute of the element are mainly used here. The file name is doHide.htm, and the code is as follows:
<html> <head> <meta http-equiv="Content-Language" content="zh-cn"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title></title> <script language="javascript"> function DoHide() { // 获得id为table1的表格,并判断它的style.display值是否等于 'block ' // 等于就执行 if 部分代码,不等于就执行 else 部分代码 if(document.getElementById("table1").style.display=='block') { // 把 id 为 table1的元素的设置为隐藏 document.getElementById("table1").style.display='none'; // 把按钮(id 为 B3 )的文字设置成 '显示表格' document.getElementById("B3").value="显示表格"; } else { // 把 id 为 table1的元素的设置为显示 document.getElementById("table1").style.display='block'; // 把按钮(id 为 B3 )的文字设置成 '隐藏表格' document.getElementById("B3").value="隐藏表格"; } } </script> </head> <body> <p><input type="button" value="隐藏表格" name="B3" onclick="DoHide();"></p> <table border="1" width="100%" id="table1" style="display:block"> <tr> <td>这是一个表格</td> <td>单元格2</td> <td>单元格3</td> <td>单元格4</td> <td>单元格5</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> <td>4</td> <td>5</td> </tr> <tr> <td>2</td> <td>2</td> <td>2</td> <td>2</td> <td>2</td> </tr> </table> </body> </html>
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to hide the table in javascript. For more information, please follow other related articles on the PHP Chinese website!