在 JavaScript 中隐藏“div”标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12107671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:07:12  来源:igfitidea点击:

Hide 'div' tag in JavaScript

javascripthtmldreamweaver

提问by Undefined

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

我有 JavaScript 代码,当它被点击时隐藏一个标签:

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?

虽然当我这样做时,即使隐藏了 div 标签,仍然有 div 标签所在的空间。如何使用 JavaScript 折叠整个 div 标签?

回答by xdazz

Use:

用:

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

回答by Undefined

You should use:

你应该使用:

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


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

只是提到getElementById()将寻找divid 为div,我建议您将其更改为更明显的内容,例如:

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

Then your JavaScript could be:

那么你的 JavaScript 可能是:

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

Check hereto see what the difference is between display:noneand visibility:hidden

检查这里看看有什么区别之间display:nonevisibility:hidden

回答by Sandy8086

Try this ..

尝试这个 ..

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

回答by Nikhil D

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

回答by Ahsan Khurshid

Use

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

OR

或者

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

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

注意:document.getElementById()只选择具有id属性的元素。