javascript - 如何用 Javascript 隐藏表格?

标签 javascript css

我正在尝试创建一个隐藏表。当您单击一个按钮时,它应该出现在页面上,然后当您再次单击该按钮时,它应该会消失。问题是,这段代码不起作用:

这就是JS的功能

function showtable1() {
  var x = document.getElementById("table");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

这是 HTML 代码:

<body>
  <video autoplay loop muted id="backgroundv1">
    <source src="fp.mp4" type="video/mp4">
  </video>
  <div class="menü1">
    <button class="button" onclick="showtable1()">Project  1</button>
    <button class="button">Project  2</button>
    <button class="button">Project  3</button>
    <button class="button">Project  4</button>
    <button class="button">Project  5</button> 
  </div>
  <table class="table1"><td></td> <td></td> </table>
</body>

如果您需要 CSS 代码:

.table1 {
  width: 64%;
  height: 50%;
  border: 2px solid red;
  border-radius: 12px;
  margin-left: auto;
  margin-right: auto;
  transform: translate(0);
}

最佳答案

你的代码中的问题是在html标记中,你的表没有id,在表标签中添加id="table"

替换

 <table class="table1"><td></td> <td></td> </table>

 <table id="table"><td></td> <td></td> </table>

你的代码应该可以工作

关于javascript - 如何用 Javascript 隐藏表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56742859/

相关文章:

javascript - 带有事件背景元素的 Angular Bootstrap $modal

javascript - 关于学习网络编程

javascript - 处理可选参数的 Node 模块

html - IE7 无序列表中的奇怪 CSS 行为

css - ionic ionic 文本换行 : remove elipsis and go to next line for label

html - 尝试使用 Grid 或 Flexbox 获得具有小间距间隙的相等列

html - 水平溢出多个内联垂直列表

html - Chrome Print - 从页脚中删除 URL 但保留页码?

JavaScript:在 hashchange 上按名称调用函数

javascript - javascript中关联多维数组赋值错误