简体   繁体   English

用JS显示隐藏的div

[英]Show a hidden div with JS

I have done a CSS for a hidden div. 我已经为隐藏的div做过CSS。 I'm trying to show the div when a button is clicked, but I'm getting "null" on Mozilla debugger. 我试图在单击按钮时显示div,但在Mozilla调试器上却显示为“ null”。 Any tip to solve this? 有解决这个问题的提示吗? Thanks. 谢谢。

Sorry if the code is so bad, im just learning a little on my free time. 对不起,如果代码太糟糕了,我只是在空闲时间学了一点。

<script id="but1">

  function Show() {
    var ele = document.getElementById("box");
    ele.style.visibility= "visible" ;
  }
</script>

<button type="button" class="but1" id="but1" onclick="Show()">Start</button> 
<div class="box">
  <p class="clicked">Content</p>
</div>

As you have element with class , not id you should use document.getElementsByClassName("box")[0]; 因为您的元素具有class ,而不是id ,所以应该使用document.getElementsByClassName("box")[0]; instead document.getElementById("box"); 而是document.getElementById("box");

I have done a CSS for a hidden div . 我已经为隐藏的div做过CSS。

In that case, use display:block instead of ele.style.visibility= "visible" ; 在这种情况下,请使用display:block而不是ele.style.visibility= "visible" ;

Also, box is a class so document.getElementById wont work. 另外, box是一个类,因此document.getElementById将无法工作。

var ele = document.querySelector(".box");
ele.style.display= "block" ;

您可以使用querySelector而不是getElementById来标识id而不是类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM