简体   繁体   English

隐藏除特定子元素内容以外的所有内容

[英]Hide All Except Specific Child Element Content

How would I hide all of a certain element's contents excluding one specific child's contents? 我如何隐藏某个特定元素的所有内容(不包括一个特定孩子的内容)?

Using display hides the child. 使用display隐藏孩子。

I want them visible and using visibility keeps the space of the hidden content. 我希望它们可见,并且使用visibility可以保留隐藏内容的空间。

Here is what I have: 这是我所拥有的:

 #test1 { visibility: hidden } #test2 { visibility: visible } #test3 { display: none } #test4 { display: block } 
 <div id="test1"> <div id="test2">test2</div> test1 </div> <div id="test3"> <div id="test4">test4</div> test3 </div> 

  • you give a general class to the parent (if you don't to use the HTML tag), and apply it the property visibility:hidden and height:0 您给父class提供一个通用class (如果您不使用HTML标记),然后将其应用到属性visibility:hiddenheight:0
  • apply to all children height:inherit (so it will apply 0 as parent has it) 适用于所有孩子的height:inherit (因此,它将适用于父级拥有的0
  • either by ID or class , whatever you feel most comfortable, you set the child you want to show visibility: visible and height:auto 无论是按ID还是按class ,无论您觉得最舒适如何,都可以设置要显示visibility: visible的孩子visibility: visibleheight:auto

 .hidden { visibility: hidden; height: 0; } .hidden > div { height: inherit; } .hidden .show { visibility: visible; height: auto; border: 1px dashed red; } 
 <div class="hidden" id="test1"> <div class="show" id="test2">test2 - i'm the only one visible!</div> test1 </div> <div class="hidden" id="test3"> <div id="test4">test4</div> test3 </div> <div class="hidden" id="test5"> <div id="test6">test5</div> test6 </div> 

Try something like this: 尝试这样的事情:

#test1 > * { visibility:hidden; }
#test1 > #test2 { visibility:visible; }

#test3 > * { display:none; }
#test3 > #test4 { display:block; }
<div id="test1">
    <div id="test2">
        <p>test2</p>
    </div>
    <p>test1</p>
</div>
<div id="test3">
    <div id="test4">
        <p>test4</p>
    </div>
    <p>test3</p>
</div>

The essence here is, that you can't hide a parent, and still show a child. 这里的本质是,您不能隐藏父母,而仍然可以生孩子。 The parent needs to be visible, and you must instead hide the unwanted child-elements, while only showing the one you need. 父项必须可见,而您必须隐藏不需要的子项,而仅显示所需的子项。

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

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