简体   繁体   English

如何在网页加载时隐藏div

[英]How to hide div on load of webpage

I have a basic script which shows/hides a div. 我有一个显示/隐藏div的基本脚本。 I'm using this for a drop-down menu. 我将其用于下拉菜单。

https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp https://www.w3schools.com/howto/howto_js_toggle_hide_show.asp

I'm looking for the div element to be hidden when the page loads instead of it showing and having to click it to toggle it. 我正在寻找页面加载时隐藏的div元素,而不是显示并不得不单击以切换它。

Any help is appreciated! 任何帮助表示赞赏!

Use display: none in div like below 使用display: none div中display: none ,如下所示

<div id="myDIV" style="display:none">
  This is my DIV element.
</div>

or you can create a class and assign to the div. 或者您可以创建一个类并分配给div。

<style>
.hide{
   display:none;
}
</style>


<div id="myDIV" class="hide">
  This is my DIV element.
</div>

Instead of CSS, you may also use JavaScript to manipulate the display of a web page by taking advantage of events, such as onload. 除了CSS,您还可以使用JavaScript通过利用事件(例如onload)来操纵网页的显示。

 window.onload = function(){ document.getElementById("myDIV").style.display='none'; }; 
 <div> This is the first DIV element. </div> <div id="myDIV"> This is the 2nd DIV element. </div> <div> This is the last DIV element. </div> 

Using the following methods, you tell the browser to ignore these elements on the page. 使用以下方法,您可以告诉浏览器忽略页面上的这些元素。

Use display none on the element eighter in HTML, or in CSS files. 在HTML或CSS文件中的8号元素上使用display none。

 <div style="display: none"> This is my DIV element. </div> 

Attribute hidden is also helpful. 隐藏属性也很有帮助。

 <div hidden> This is my DIV element. </div> 

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

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