Home > Web Front-end > JS Tutorial > body text

How to hide the table in javascript

藏色散人
Release: 2023-01-05 16:11:45
Original
4493 people have browsed it

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.

How to hide the table in javascript

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值是否等于 &#39;block &#39;
// 等于就执行 if 部分代码,不等于就执行 else 部分代码
if(document.getElementById("table1").style.display==&#39;block&#39;)
{
// 把 id 为 table1的元素的设置为隐藏
document.getElementById("table1").style.display=&#39;none&#39;;
// 把按钮(id 为 B3 )的文字设置成 &#39;显示表格&#39;
document.getElementById("B3").value="显示表格";
}
else
{
// 把 id 为 table1的元素的设置为显示
document.getElementById("table1").style.display=&#39;block&#39;;
// 把按钮(id 为 B3 )的文字设置成 &#39;隐藏表格&#39;
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>
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template