默认情况下如何隐藏div?

麦克风

当前,以下脚本默认情况下显示内容。如何调整代码,使其默认情况下处于隐藏状态,为了隐藏或显示它,必须单击按钮。

<script>
function myFunction_1() {
  var x = document.getElementById("myDIV");
  var y = document.getElementById("myDIV2");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

function myFunction_2() {
  var y = document.getElementById("myDIV2");
  if (y.style.display === "none") {
    y.style.display = "block";
  } else {
    y.style.display = "none";
  }
}
<script>

#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}

<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
<button onclick="myFunction_1()">Try it</button>
<div id="myDIV">
  This is my DIV element.
</div>
<br><br>
<button onclick="myFunction_2()">Try it 2</button>
<div id="myDIV2">
  This is my DIV element.
</div>
<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>
iota

只需display: none在每个元素上设置为内联样式即可。

<div id="myDIV" style="display: none;">
  This is my DIV element.
</div>
<br><br>
<button onclick="myFunction_2()">Try it 2</button>
<div id="myDIV2" style="display: none;">
  This is my DIV element.
</div>

现场示例:

function myFunction_1() {
  var x = document.getElementById("myDIV");
  var y = document.getElementById("myDIV2");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}

function myFunction_2() {
  var y = document.getElementById("myDIV2");
  if (y.style.display === "none") {
    y.style.display = "block";
  } else {
    y.style.display = "none";
  }
}
#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>
<button onclick="myFunction_1()">Try it</button>
<div id="myDIV" style="display: none;">
  This is my DIV element.
</div>
<br><br>
<button onclick="myFunction_2()">Try it 2</button>
<div id="myDIV2" style="display: none;">
  This is my DIV element.
</div>
<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

默认情况下如何使UISearchController()隐藏?

默认情况下如何隐藏列?

在上拉我的网站时,默认情况下如何隐藏<div>元素?

Gnuplot:默认情况下隐藏图?

默认情况下,菜单应隐藏

VUE JS - 默认情况下如何隐藏 div?如果用户点击按钮,那么它应该使用 vue 显示

如何使用 jQuery 显示/隐藏隐藏的 - 默认情况下 - 表格行?

默认情况下隐藏div并在使用引导程序单击时显示

每次打开Chrome DevTools时,默认情况下如何隐藏“控制台抽屉”?

默认情况下如何显示隐藏字符(零宽度空间,即&#8203)

默认情况下如何在Nautilus中隐藏侧面板?

默认情况下如何在引导程序中隐藏侧边栏?

Emacs组织模式:默认情况下如何隐藏明细列表项的详细信息

默认情况下如何隐藏Node.js中的内部堆栈跟踪错误?

默认情况下如何显示ionicPopover

默认情况下如何使Windows透明

默认情况下如何打开菜单?

默认情况下切换点击隐藏

如何在不影响其他div的情况下隐藏/显示匿名div

如何在没有“隐藏的div”或“不显示”的情况下切换内容?

如何在不占用空间的情况下使用CSS包含隐藏的DIV?

在我的情况下如何滚动div?

默认情况下将div设置为活动

Spring Data REST默认情况下从JSON隐藏技术实体字段(@ Version,@ Id)。如何返回它们的常规属性?

默认情况下,如何在 ggplotly 分组时间序列图中隐藏单行?(情节,R,Javascript)

默认情况下如何在不加载片段的情况下使用导航图?

在这种情况下,如何隐藏HTML / JavaScript代码?

在我的情况下,如何正确显示/隐藏按钮?

默认情况下如何启动tmux执行命令?