SASS:将伪类添加到祖辈符号和

SASS:将伪类添加到祖辈符号和,sass,Sass,这是SASS代码 @mixin test { @at-root #{selector-replace(&, '.class1', '.class1:nth-child(odd)')} { color:red; } } .class1 { .class2 { @include test; } } …编译为: .class1:nth-child(odd) .class2 { color: red; } .class1:nth-child(o

这是SASS代码

@mixin test
{
  @at-root #{selector-replace(&, '.class1', '.class1:nth-child(odd)')}
  {
    color:red;
  }
}

.class1
{
  .class2
  {
     @include test;
  }
}
…编译为:

.class1:nth-child(odd) .class2
{
  color: red;
}
.class1:nth-child(odd) .class2 {
  color: red;
}
当我不知道如何调用class1而不使用选择器替换时,这可能吗

我只想给祖父母添加第n个子选择器


我只允许更改mixin,而不允许更改原始代码。

好的,这将完成以下操作:

@mixin test
{

  // Find the first selector
  $parent : nth(nth(&, 1), 1);

  // Defines a list for the rest of the selectors
  $rest : ();

  // For each selector of &, starting from the second
  @for $i from 2 through length(nth(&, 1)) {

    // Adds the selector to the list of the "rest of the selectors"
    $rest: append($rest, nth(nth(&, 1), $i));

  }

  // Adds the selector at root
  @at-root #{$parent}:nth-child(odd) #{$rest} {
    color: red;
  }

}

.class1
{
  .class2
  {
    @include test;
  }
}
这将编译为:

.class1:nth-child(odd) .class2
{
  color: red;
}
.class1:nth-child(odd) .class2 {
  color: red;
}

希望有帮助

必须在内部调用mixin。class2?是的。class2是一个按钮,具有mixin提供的特定样式。每第二个1类物品上都应该是红色的。