简体   繁体   English

CSS 选择组合多个选择器

[英]CSS selecting combining multiple selectors

(Not quite related to CSS "and" and "or" , or Is there a CSS parent selector? , as I am trying to achieve different goal here - either and ing two selectors or creating a different HTML structure that can be used by css to solve my problem) (Not quite related to CSS "and" and "or" , or Is there a CSS parent selector? , as I am trying to achieve different goal here - either and ing two selectors or creating a different HTML structure that can be used by css解决我的问题)

Hi, I wanted to know if there is any plausible way in CSS to make a logical AND between selectors.嗨,我想知道 CSS 中是否有任何合理的方法可以在选择器之间进行逻辑AND This seems quite easy when I only refer to one DOM element, but when I try referring to many, the whole concept becomes much harder.当我只引用一个 DOM 元素时,这似乎很容易,但当我尝试引用许多时,整个概念变得更加困难。

I'll give my example:我举个例子:

I have the following HTML structure, representing an accordion:我有以下 HTML 结构,代表手风琴:

<div id="holder">
    <div id="toggle">
        <div id="content"></div>
    </div>
    <div id="data" class="show|hide">
    </div>
</div>

(BTW, I am using the bootstrap logic for the accordion, so I prefer not changing a lot in the given structure) (顺便说一句,我正在使用手风琴的引导逻辑,所以我不喜欢在给定的结构中进行太多更改)

I want to set #content::after depending on whether hide or show are the active state.我想设置#content::after取决于hideshow是活动的 state。

This could be converted to the following statement:这可以转换为以下语句:

#hodler > #data.show && #holder > #toggle > #content::after

However, this is an invalid syntax.但是,这是无效的语法。 Moreover, CSS does not parent selector.此外,CSS 没有父选择器。 Any solutions?有什么解决办法吗?

Thanks!谢谢!

This is not possible with just CSS and the current HTML.仅使用 CSS 和当前的 HTML 是不可能的。

If the order of the #toggle and #data elements were reversed, you could use the adjacent sibling combinator ( + ) like this:如果#toggle#data元素的顺序颠倒了,您可以像这样使用相邻的同级组合符 ( + )

<div id="holder">
    <div id="data" class="show|hide"></div>
    <div id="toggle">
        <div id="content"></div>
    </div>
</div>
#data.show + #toggle #content:after { /* show styles */ }
#data.hide + #toggle #content:after { /* hide styles */ }

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

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