仅使用 css 显示隐藏 div
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17042279/
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
Show Hide div with css only
提问by acctman
I'm trying to do a show / hide using css only, is that possible or does some type of jscript is needed? this is what i'm trying to do, when anyone one of the 4 div's are clicked the div for it below is shown.
我正在尝试仅使用 css 进行显示/隐藏,这可能还是需要某种类型的 jscript?这就是我想要做的,当 4 个 div 中的任何一个被点击时,下面会显示它的 div。
<div class="span3">
<img src="an1.jpg" class="img-rounded" />
<h3>AN1<br />1234</h3>
</div>
<div class="span3">
<img src="an2.jpg" class="img-rounded" />
<h3>AN2<br />1234</h3>
</div>
<div class="span3">
<img src="an3.jpg" class="img-rounded" />
<h3>AN3<br />1234</h3>
</div>
<div class="span3">
<img src="an4.jpg" class="img-rounded" />
<h3>AN4<br />1234</h3>
</div>
Show div when div is click:
单击 div 时显示 div:
<div style="display: none;"> this is AN1 </div>
<div style="display: none;"> this is AN2 </div>
<div style="display: none;"> this is AN3 </div>
<div style="display: none;"> this is AN4 </div>
回答by Explosion Pills
You can use a hidden input that can be toggled that corresponds to the label in the click area.
您可以使用与单击区域中的标签相对应的可以切换的隐藏输入。
<div class="span3">
<label for="an1">
<img src="an1.jpg" class="img-rounded" />
</label>
<h3><label for="an1">AN1<br />1234</label></h3>
</div>
...
<input id="an1" type=checkbox><div style="display: none;"> this is AN1 </div>
Then in your CSS:
然后在你的 CSS 中:
[type=checkbox] {
display: none;
}
:checked + div {
display: block !important;
}
I would also stear clear of style
and just use the stylesheet.
我也会避免style
使用样式表。
回答by David says reinstate Monica
In most browsers you should simply use the href
attribute of the a
elements and an id
for the element to show:
在大多数浏览器中,您应该简单地使用元素的href
属性和a
元素的 anid
来显示:
<a href="#div1">Div one</a>
<a href="#div2">Div two</a>
<a href="#div3">Div three</a>
<a href="#div4">Div four</a>
<div id="content">
<div id="div1">This is div one</div>
<div id="div2">This is div two</div>
<div id="div3">This is div three</div>
<div id="div4">This is div four</div>
</div>
Coupled with the CSS:
结合CSS:
#content > div {
display: none;
}
#content > div:target {
display: block;
}
In the HTML the wrapper div
(#content
) isn't necessary, it's simply to make it easier to specifically target those elements it contains (you could, of course, simply use a class
instead).
在 HTML 中,包装器div
( #content
) 不是必需的,它只是为了更容易地专门针对它包含的那些元素(当然,您可以简单地使用 aclass
代替)。
To add hiding functionality (to hide all, rather than just hide the sibling div
s when showing another), unfortunately requires a link to trigger a hash-change (in order to stop the :target
selector matching the div
s), which in turn requires a link (either in each of the div
s to show or elsewhere in the document, either linking elswhere (as follows):
要添加隐藏功能(隐藏所有div
s ,而不是在显示另一个时只隐藏同级s),不幸的是需要一个链接来触发哈希更改(为了停止:target
匹配div
s的选择器),而这又需要一个链接(无论是在div
要显示的每个s 中,还是在文档的其他地方,要么链接 elswhere(如下所示):
<ul id="andHideAgain">
<li><a href="#div1">Div one</a></li>
<li><a href="#div2">Div two</a></li>
<li><a href="#div3">Div three</a></li>
<li><a href="#div4">Div four</a></li>
</ul>
<div id="content">
<div id="div1">This is div one <a class="hide" href="#andHideAgain">hide</a></div>
<div id="div2">This is div two <a class="hide" href="#andHideAgain">hide</a></div>
<div id="div3">This is div three <a class="hide" href="#andHideAgain">hide</a></div>
<div id="div4">This is div four <a class="hide" href="#andHideAgain">hide</a></div>
</div>
JS Fiddle demo (link in each div
).
Or the simple use of just a hash:
或者只是一个散列的简单使用:
- Div one
- Div two
- Div three
- Div four
- hide
- 一格
- 二区
- 三区
- 四格
- 隐藏
<div id="content">
<div id="div1">This is div one</div>
<div id="div2">This is div two</div>
<div id="div3">This is div three</div>
<div id="div4">This is div four</div>
</div>
回答by nur farazi
this is how i solve show hide problem with just
这就是我解决显示隐藏问题的方法
here is my div with its class declared
这是我的 div 声明了它的类
<div class='loading'> </div>
and here is the javascript that
这是javascript
$('.loading').hide();
and
和
$('.loading').css("display", "block");