Variables 循环中的第N个子循环?
我试图创建一个循环来创建多个具有匹配内容的第n个子选择器:Variables 循环中的第N个子循环?,variables,for-loop,sass,Variables,For Loop,Sass,我试图创建一个循环来创建多个具有匹配内容的第n个子选择器: $show-numbers: true; @if $show-numbers { @for $i from 1 through 5 { &:nth-child(1) { &:before { content: '#{$i}'; } } } } 当然,这是5份 ul.checkout-bar li:nth-child(1):before { cont
$show-numbers: true;
@if $show-numbers {
@for $i from 1 through 5 {
&:nth-child(1) {
&:before {
content: '#{$i}';
}
}
}
}
当然,这是5份
ul.checkout-bar li:nth-child(1):before {
content: "1";
}
“内容”正确递增。但是我不能得到第n个子值的增量。这在Sass中不可能吗
注意静态变量可以插值:
$foo: 1;
&:nth-child(#{$foo}) {
&:before {
content: '1';
}
}
这个很好用。这是我尝试的第一件事
但是,在
for
循环中使用$i
时,它不起作用。您需要将$i
在:nth-child()
中包装为整数,如下所示:
$show-numbers: true;
@if $show-numbers {
@for $i from 1 through 5 {
&:nth-child(#{$i}) {
&:before {
content: '#{$i}';
}
}
}
}
呈现:
:nth-child(1):before {
content:'1';
}
:nth-child(2):before {
content:'2';
}
:nth-child(3):before {
content:'3';
}
:nth-child(4):before {
content:'4';
}
:nth-child(5):before {
content:'5';
}
它不是复制品。这是指变量的一次使用。我的问题是关于FOR循环中的自动递增变量。你链接上的答案不起作用,我已经试过了。考虑到你甚至没有在选择器中引用变量,我认为你还没有试过。此外,您声称从提供的代码中获得的输出是不可能的。这是输出,证明我“试过了”我可能在某个地方打错了,我发誓我在sassmeister中试了5次,它抛出了一个错误。谢谢你的帮助。请不要回答重复的问题。