简体   繁体   中英

Hide 'div' tag in JavaScript

I have JavaScript code that hides a tag when it is clicked:

document.getElementById("div").style.visibility="hidden";

Although when I do this, even though the div tag is hidden, there is still a space where the div tag is located. how do I collapse the whole div tag using JavaScript?

采用:

document.getElementById("div").style.display = 'none';

You should use:

document.getElementById("div").style.display = "none";

Just to mention that getElementById() will be looking for a div with the id of div , I suggest you change this to something more obvious, an example would be:

<div id="container"><!--Content--></div>

Then your JavaScript could be:

document.getElementById("container").style.display = "none";  

Check here to see what the difference is between display:none and visibility:hidden

尝试这个 ..

 document.getelementById("div_id").style.display = 'none';
document.getElementById("yourdivID").style.display = 'none';

Use

document.getElementById("divID").style.display = "none";

OR

document.getElementsByTagName("div").style.display = "none";

NOTE: document.getElementById() only select the elements having id attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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