组合Sass混合素选择器

组合Sass混合素选择器,sass,Sass,我试图弄清楚是否可以组合mixin选择器字符串。我不相信这在我的代码环境中是可能的,但我很可能遗漏了一些东西 假设我有以下SCS: // Apply a set of rules to input form fields. @mixin input-form-fields { input:not([type="hidden"]), textarea { @content; } } // Apply a set of rules to button for

我试图弄清楚是否可以组合mixin选择器字符串。我不相信这在我的代码环境中是可能的,但我很可能遗漏了一些东西

假设我有以下SCS:

// Apply a set of rules to input form fields.
@mixin input-form-fields {
    input:not([type="hidden"]),
    textarea {
        @content;
    }
}

// Apply a set of rules to button form fields.
@mixin button-form-fields {
    button, button {
        @content;
    }
}

// Apply a set of rules to select form fields.
@mixin select-form-fields {
    select {
        @content;
    }
}

// Apply a set of rules to all form fields.
@mixin all-form-fields {
    @include input-form-fields {
        @content;
    }
    @include button-form-fields {
        @content;
    }
    @include select-form-fields {
        @content;
    }
}
基本上,所有表单字段mixin将调用其他mixin,从而为不同的选择器生成相同的规则集

如果我编译以下代码:

@include all-form-fields {
    margin-bottom: .5em;
}
我会得到这样的结果:

input:not([type="hidden"]),
textarea {
  margin-bottom: .5em;
}

button, 
.button {
  margin-bottom: .5em;
}

select {
  margin-bottom: .5em;
}
这并不理想,如果我能将这些选择器组合起来,我会很高兴


有没有人对我如何组合3个不同mixin返回的选择器字符串有什么想法?

如果您不介意将选择器存储在字符串中,您可以使用变量定义不同的字段类型:

$input-form-fields: "input:not([type=hidden]), textarea";
$button-form-fields: "button";
$select-form-fields: "select";
然后使用插值字符串定义混合,如下所示:

// Apply a set of rules to input form fields.
@mixin input-form-fields {
    #{$input-form-fields} {
        @content;
    }
}

// Apply a set of rules to button form fields.
@mixin button-form-fields {
    #{$button-form-fields} {
        @content;
    }
}

// Apply a set of rules to select form fields.
@mixin select-form-fields {
    #{$select-form-fields} {
        @content;
    }
}

// Apply a set of rules to all form fields.
@mixin all-form-fields {
    #{$input-form-fields}, 
    #{$button-form-fields}, 
    #{$select-form-fields} {
        @content;
    }
}
因此,
@包含所有表单字段将导致

input:not([type=hidden]), textarea,
button,
select {
  margin-bottom: .5em; }

非常感谢。是的,@cimmanon发布了重复的线程后,我像你一样给出了确切的解决方案。感谢您发布解决方案。