简体   繁体   English

如何使用属性选择器更改子元素的CSS

[英]How to change CSS of child element using attribute selector

I want to hide a div when it belongs to a parent div which has the attribute data-ontarget set to true. 我想隐藏一个div,它属于父div,其属性data-ontarget设置为true。 I tried the following, but nothing happens. 我尝试了以下,但没有任何反应。

If I remove the class name after the attribute selector in the example below, the whole parent div becomes hidden. 如果我在下面的示例中删除属性选择器后面的类名,则整个父div变为隐藏。 So the attribute selector works, but not in combination with the search for a child class. 因此,属性选择器可以工作,但不能与搜索子类相结合。

Here is what I tried: HTML: 这是我尝试过的:HTML:

<div class="myClass" data-ontarget="false">
        <img src="myImage1.png"/>
        <div class="myImage">This is my image 1</div>
</div>
<div class="myClass" data-ontarget="true">
        <img src="myImage2.png"/>
        <div class="myImage">This is my image 2</div>
</div>

CSS: CSS:

[data-ontarget="true"] .myImage{
    display: none;
}

Replace opacity property with display to hide the element. display替换opacity属性以隐藏元素。

 .input_container[data="true"] .awsome_input_border{ opacity: .5; } .input_container[data="false"] .awsome_input_border{ opacity: 1; } 
 <div class="input_container" data="true"> <span class="awsome_input_border"/> hide me!! </div> <div class="input_container" data="false"> <span class="awsome_input_border"/> hide me!! </div> 

I think you are missing the proper CSS, which makes your target div as hidden. 我认为你错过了正确的CSS,这使你的目标div隐藏起来。

Refer to the demo here . 请参阅此处的演示。

Please find the code below: 请找到以下代码:

HTML: HTML:

<div class="parent">
  <div class="myClass" data-ontarget="false">
    <img src="myImage1.png" />
    <div class="myImage">This is my image 1</div>
  </div>
  <div class="myClass" data-ontarget="true">
    <img src="myImage2.png" />
    <div class="myImage">This is my image 2</div>
  </div>
</div>

CSS: CSS:

.parent > div[data-ontarget="true"] {
  display: none;
}

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

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