更改选择器之外的容器。

huangapple go评论73阅读模式
英文:

Change container "outside" of the selector

问题

我有一个复杂的网站,由多个容器组成。我的目标是检查一个 div 容器上的一个类是否为 "active",如果是,我需要对另一个位于该容器之外的容器进行着色。所以代码看起来像这样:

<div class="tooltip">
  <tooltip>
    <div class="active">
      My tooltip is active
    </div>
  </tooltip>
</div>
<div class="othercontainer">
</div>
<main-view>
  <div class="colorme">
    Test
  </div>
</main-view>

我想在 <tooltip> 标签具有类名为 active 的容器时对具有类名为 colorme 的 div 进行着色。我不确定这是否在一般情况下可行,但我认为我可以构建类似这样的东西。

英文:

I have a complex websites which consists of containers. My goal is to check a class on a div container if the class is "active" and if so, I need to color another container which is "far" outside of that container. So the Code looks like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.tooltip:has(tooltip &gt; div.active)~main-view&gt;div.colorme {
  background: orange
}

<!-- language: lang-html -->

&lt;div class=&quot;tooltip&quot;&gt;
  &lt;tooltip&gt;
    &lt;div class=&quot;active&quot;&gt;
      My tooltip is active
    &lt;/div&gt;
  &lt;/tooltip&gt;
&lt;/div&gt;
&lt;div class=&quot;othercontainer&quot;&gt;
&lt;/div&gt;
&lt;main-view&gt;
  &lt;div class=&quot;colorme&quot;&gt;
    Test
  &lt;/div&gt;
&lt;/main-view&gt;

<!-- end snippet -->

And I want to color the div with class colorme as soon as the &lt;tooltip&gt; tag has a container with class active. I am not sure if this is in general possible, but I thought I could build something like this.

答案1

得分: 1

很遗憾,由于目前对于:has CSS选择器的支持并不是很好,所以这个问题实际上不太可能实现。可以在这里查看支持情况表格:https://caniuse.com/css-has - 没有Firefox的支持,而Firefox在浏览器市场份额中占据了相当大的比例。

除此之外,你的逻辑似乎是正确的。但是为了更好地实现这个功能,可以考虑向父元素添加/删除一个类。这样做会更容易理解,并且需要更简单的CSS。

<div id="wrapper" class="has-active-tooltip">
  <div class="tooltip">
    <tooltip>
      <div class="active">
      我的工具提示是激活的
      </div>  
     </tooltip>
  </div>
  <div class="othercontainer"></div>
  <main-view>
   <div class="colorme">
    测试
   </div>
  </main-view>
</div>
#wrapper.has-active-tooltip main-view > .colorme {
  /*在这里添加样式*/
}
英文:

Unfortunately this won't really be possible since the :has CSS selector is not really well supported right now. See the support table here: https://caniuse.com/css-has - no Firefox support, and that's a pretty big piece of the browser market share.

Other than that, your logic seems sound. But to get this working in a better way might be to add/remove a class to a parent element. This will be easier to understand, and require less complex CSS

&lt;div id=&quot;wrapper&quot; class=&quot;has-active-tooltip&quot;&gt;
  &lt;div class=&quot;tooltip&quot;&gt;
    &lt;tooltip&gt;
      &lt;div class=&quot;active&quot;&gt;
      My tooltip is active 
      &lt;/div&gt;  
     &lt;/tooltip&gt;
  &lt;/div&gt;
  &lt;div class=&quot;othercontainer&quot;&gt;&lt;/div&gt;
  &lt;main-view&gt;
   &lt;div class=&quot;colorme&quot;&gt;
    Test
   &lt;/div&gt;
  &lt;/main-view&gt;
&lt;/div&gt;
#wrapper.has-active-tooltip main-view &gt; .colorme {
  /*stuff here*/
}

答案2

得分: 0

你可以使用父容器,并使用 :has() 伪选择器,如下所示:

.common-parent:has(tooltip .active) main-view .colorme{
    color: orangered;
}
<div class="common-parent">
    <div class="tooltip">
        <tooltip>
            <div class="active">
                My tooltip is active
            </div>
        </tooltip>
    </div>

    <div class="othercontainer">
    </div>

    <main-view>
        <div class="colorme">
            Test
        </div>
    </main-view>
</div>
英文:

You can use a parent container and then use :has() pseudo selector like below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.common-parent:has(tooltip .active) main-view .colorme{
    color: orangered;
}

<!-- language: lang-html -->

&lt;div class=&quot;common-parent&quot;&gt;
    &lt;div class=&quot;tooltip&quot;&gt;
        &lt;tooltip&gt;
            &lt;div class=&quot;active&quot;&gt;
                My tooltip is active
            &lt;/div&gt;
        &lt;/tooltip&gt;
    &lt;/div&gt;

    &lt;div class=&quot;othercontainer&quot;&gt;
    &lt;/div&gt;

    &lt;main-view&gt;
        &lt;div class=&quot;colorme&quot;&gt;
            Test
        &lt;/div&gt;
    &lt;/main-view&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月8日 23:11:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定