Using a variable in a selector in LESS

非 Y 不嫁゛ 提交于 2020-01-10 19:30:28

问题


I have a variable @index and I want to output a selector so that when @index is 3, the selector is [data-sth="3"].

Longer example:

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

Desired output:

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

I've tried a few things, but haven't managed to get anything that works, yet.

Thanks.


回答1:


See Variable Interpolation. I.e.:

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

Original answer (Less versions before 1.40):

The following code should work:

(~'[data-sth="@{index}"]') { color: red; }

The ~ removes the quotation marks.



来源:https://stackoverflow.com/questions/10154595/using-a-variable-in-a-selector-in-less

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!