在LESS中使用变量作为选择器

7

我有一个变量@index,我想输出一个选择器,以便当@index3时,选择器为[data-sth="3"]

更长的示例:

@index: 3;
/* selector here */ {
    color: red;
}

期望的输出:

[data-sth="3"] {
    color: red;
}

我尝试过几种方法,但都没有成功过。谢谢。
1个回答

9
请参见变量插值功能。即:
[data-sth="@{index}"] {
    color: red;
}

以下代码应该可以工作:
(~'[data-sth="@{index}"]') { color: red; } ~ 删除了引号。

1
谢谢!括号也很重要。 - Yuji 'Tomita' Tomita

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接