LESS | 目标元素无特定父类

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

LESS | Target elements without specific parent class

问题

I want to target the elements with class list__item--draggable that do not have a parent/grandparent with the class list__item--draggable i.e. the element with the text "List item 1". I want to achieve this using LESS. I do not want to use the > selector because it can be a nested structure with few more divs and other elements.

我想要选择具有类list__item--draggable的元素,但其父元素/祖父元素没有类list__item--draggable,即具有文本"List item 1"的元素。我想使用LESS来实现这一点。我不想使用>选择器,因为它可能是一个具有更多div和其他元素的嵌套结构。

英文:

I have a HTML structure as shown below

<ul class="list list--sm">
	<li class="list__item list__item--draggable">
		List item 1
          <ul>
			  <li class="list__item list__item--draggable">
				  List item 1.1
                    <ul>
				      <li class="list__item list__item--draggable">
					       List item 1.1.1
				      </li>
                    </ul>
			  </li>
          </ul>
	</li>
</ul>

Here I want to target the elements with class list__item--draggable that do not have a parent/grandparent with the class list__item--draggable i.e. the element with the text "List item 1". I want to achieve this using LESS. I do not want to use the > selector because it can be a nested structure with few more divs and other elements.

I am not able to think of a solution for this. Can this be achieved?

答案1

得分: 1

这在纯CSS中也是可能的:

.list__item--draggable:not(.list__item--draggable *) {}

尝试一下:

<!-- 开始片段: js 隐藏: true -->

<!-- 语言: lang-css -->

.list__item--draggable {
  outline: 1px solid black;
}

.list__item--draggable:not(.list__item--draggable *) {
  outline: 1px solid red;
}

<!-- 语言: lang-html -->

<ul class="list list--sm">
  <li class="list__item list__item--draggable">
    List item 1
    <ul>
      <li class="list__item list__item--draggable">
        List item 1.1
        <ul>
          <li class="list__item list__item--draggable">
            List item 1.1.1
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

<!-- 结束片段 -->


[1]: https://caniuse.com/css-has

<details>
<summary>英文:</summary>

This is possible even in pure CSS:

```css
.list__item--draggable:not(.list__item--draggable *) {}

Try it:

<!-- begin snippet: js hide: true -->

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

.list__item--draggable {
  outline: 1px solid black;
}

.list__item--draggable:not(.list__item--draggable *) {
  outline: 1px solid red;
}

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

&lt;ul class=&quot;list list--sm&quot;&gt;
	&lt;li class=&quot;list__item list__item--draggable&quot;&gt;
		List item 1
          &lt;ul&gt;
			  &lt;li class=&quot;list__item list__item--draggable&quot;&gt;
				  List item 1.1
                    &lt;ul&gt;
				      &lt;li class=&quot;list__item list__item--draggable&quot;&gt;
					       List item 1.1.1
				      &lt;/li&gt;
                    &lt;/ul&gt;
			  &lt;/li&gt;
          &lt;/ul&gt;
	&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

答案2

得分: 0

以下是已翻译的内容:

.list__item--draggable {
  background: yellow;
}

ul ul,
ul ul .list__item--draggable {
  background: white;
}
<ul class="list list--sm">
  <li class="list__item list__item--draggable">
    List item 1
    <ul>
      <li class="list__item list__item--draggable">
        List item 1.1
        <ul>
          <li class="list__item list__item--draggable">
            List item 1.1.1
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>
英文:

It may depend on exactly how you want to style/restyle the children but here's a simple CSS example which highlights just that first text:

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

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

.list__item--draggable {
  background: yellow;
}

ul ul,
ul ul .list__item--draggable {
  background: white;
}

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

&lt;ul class=&quot;list list--sm&quot;&gt;
	&lt;li class=&quot;list__item list__item--draggable&quot;&gt;
		List item 1
          &lt;ul&gt;
			  &lt;li class=&quot;list__item list__item--draggable&quot;&gt;
				  List item 1.1
                    &lt;ul&gt;
				      &lt;li class=&quot;list__item list__item--draggable&quot;&gt;
					       List item 1.1.1
				      &lt;/li&gt;
                    &lt;/ul&gt;
			  &lt;/li&gt;
          &lt;/ul&gt;
	&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

Basically, we change every occurence, then we change back those that are children.

huangapple
  • 本文由 发表于 2023年4月11日 14:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982912.html
匿名

发表评论

匿名网友

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

确定