css - Sass:通过变量迭代链接选择器

标签 css compass-sass sass

[I'm using the Sass Indented syntax though feel free to answer via SCSS]

这是循环的嵌套部分,我想在其中使用变量为每次迭代命名选择器:

$class: 2      
@for $i from 1 through 2
  @if $i == 1
    $sel: link
  @if $i == 2
    $sel: visited
  div #s-#{$class} ul
    &:nth-child(1) li
      &:nth-child(2) a
        &:$sel
          color: $cc

但是我得到:

"Syntax error: Invalid CSS after \"&:\": expected pseudoclass or pseudoelement, was \"$sel\"\A

(是的,我可以看出这是一个语法错误)

我正在寻找这样的输出:

div #s-2 ul:nth-child(1) li:nth-child(2) a:link {
  color: #cc0000;
}

div #s-2 ul:nth-child(1) li:nth-child(2) a:visited {
  color: #cc0000;
}

如果做这样的事情,Sass 有能力吗?如果是怎么办?

最佳答案

有一种更好(更明显)的方法可以实现我想要实现的目标(以 $i 从 1 到 1 为例):

$class: 2   
@for $i from 1 through 1
  div #s-#{$class} ul
    &:nth-child(1) li
      &:nth-child(2) a
        &:visited, &:link
          color: $cc

输出

div #s-2 ul:nth-child(1) li:nth-child(2) a:visited, div #s-2 ul:nth-child(1) li:nth-child(2) a:link {
  color: #cc0000;
}

虽然我对使用选择器作为变量的方法很好奇

关于css - Sass:通过变量迭代链接选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10166850/

相关文章:

symfony1 - 如何将 Compass 与 symfony 一起使用?

css - 在不包括魔术选择器的情况下在 Compass Sprite 中引用图标的方法

html - Rails 应用程序中的 CSS 覆盖

css - 为什么伪元素周围有额外的空间?

html - IE6定位问题

jquery - 使 DIV 缓慢移出屏幕

尽管我使用的是供应商前缀,但 CSS 动画在 Firefox 或 Safari 中不起作用

html - Bootstrap 导航对齐堆栈 li

css - 在没有 Compass 的情况下使用 Sass 创建 CSS Sprite ?

sass - 我的 box-shadow SASS @Mixin 有什么问题?