SASS Mixin Rewrite& (符号)

时间:2017-05-10 17:24:19

标签: sass compass-sass compass

我正在尝试编写一个mixin来修改输出中的父选择器。我们的想法是,在调用mixin的情况下,父选择器需要对其进行字符串替换。我有大部分工作,但我无法弄清楚如何吞下&

.test {
  @include alt_parent() {
    content: 'test';
  }
}

mixin是这样的:

@mixin alt_parent() {
  #{str-replace(unquote("#{selector_append(&)}"), "s", "x")} {
    @content;
  }
}

我有字符串替换工作,所以这不是问题。我得到的是这个(我理解为什么):

.test .text {
  content: 'test';
}

我想要的是:

.text {
  content: 'test';
}

1 个答案:

答案 0 :(得分:2)

您必须使用@at-root指令来阻止自动包含&所代表的选择器。

http://alwaystwisted.com/articles/2014-03-08-using-sass-33s-at-root-for-piece-of-mind

@mixin alt_parent($parent) {
    @at-root {
        #{str-replace(unquote("#{selector_append(&)}"), "s", "x")} {
            @content;
        }
    }
}
相关问题