【发布时间】:2020-02-10 13:15:23
【问题描述】:
我正在尝试创建一个递归@mixin。 mixin 的任务是从给定列表中附加选择器并应用 CSS 规则。其结果必须如下所示:
[data-tag]:not([conref]) {
border: dashed 2px #2999d1;
}
[data-tag]:not([conref])[nodeid] {
border: dashed 2px #2999d1;
}
[data-tag]:not([conref])[nodeid][draggable] {
border: dashed 2px #2999d1;
}
[data-tag]:not([conref])[nodeid][draggable][class] {
border: dashed 2px #2999d1;
}
我创建的@mixin 如下所示:
@mixin set-border-to-selectors($list, $item){
@if false == (index($list, $item)){
@error "Fail: #{$item} not in list: #{$list}";
}
@else{
$index: index($list, $item);
@debug "index: #{$index}";
$item: nth($list, $index);
&[#{$item}]{
@include set-data-tag-border;
@if($index + 1 <= length($list)){
$item: nth($list, $index + 1);
@include set-border-to-selectors($list, $item);
}
}
}
}
我第一次使用该功能时效果很好。
@mixin show-data-tag-border{
$data-tag-list: (#{$id}, draggable, class);
$data-tag-list-two: (#{$id}, #{$conref}, class);
&[data-tag]:not([#{$conref}]){
@include set-data-tag-border;
@include set-border-to-selectors($data-tag-list, #{$id});
}
&[data-tag]{
@include set-border-to-selectors($data-tag-list-two, #{$id});
}
但是 set-border-to-selector 函数的第二次调用会抛出错误,因为 recusion @mixin 中的 $index 不包含数字。它是空的。
我从p{@include show-data-tag-border;} 调用函数,边框将设置在@mixin set-data-tag-border{border: solid 1px black}。而这两个变量:
$id: id;
$conref: conref;
我看不到有什么问题吗?或者这种方法在 SCSS 中创建递归是否有问题?我不会只使用一次这个函数,而是在下面写一个或多个类似的函数来完成这个。
【问题讨论】:
-
这里没有足够的代码来编译(缺少变量、mixin 引用等)
-
对于我使用
@mixin set-data-tag-border{ border: dashed 2px #2999d1; }的边框,调用函数可能是这样的p{@include show-data-tag-border;} -
所以更新你的问题。我们不得不把事情拼凑起来很不方便。据我们所知,您定义它们的方式是问题的一部分。
-
仍然不是我的意思。如果在sassmeister上编译不出来,说明这里代码不够。