我想在css中实现以下功能.我如何以在IE8 +中运行的方式执行此操作
url('../img/icons/' + attr('type') + '_10.png')
Nie*_*sol 21
我认为你不能.在content属性中,您可以通过用空格分隔来"连接",但在其他地方我不认为有这样的功能.这是一种耻辱.
style每当type使用该属性时,您最好在属性中指定此样式.
小智 15
CSS 在不使用任何运算符(例如 +、& 等)的情况下执行连接。将字符串放在引号中,将字符串、attr、var 等合并为一行。
例子:
url('not/very' '/useful/concatenation'); // not/very/useful/concatentationurl('../img/icons/' attr('type') '_10.png'); //../img/icons/${type}_10.pngurl(attr('href') '#hash'); // https://${href}/#hashurl(var(--hello) ' world'); // Hello World不,你不能在纯CSS中这样做,因为CSS语言没有控制结构或类似的东西,它将允许你动态地生成CSS代码.
相反,您可以使用javascript解决方案或基于PHP编码的CSS变量的解决方案.
您无法按照您建议的方式进行动态字符串插值,但如果属性的可能值数量有限[type],则可以为每个值创建样式:
.your .selector[type="foo"] {
    background-image: url('../img/icons/foo_10.png');
}
.your .selector[type="bar"] {
    background-image: url('../img/icons/bar_10.png');
}
.your .selector[type="baz"] {
    background-image: url('../img/icons/baz_10.png');
}
如果您的类型数量不合理,那么您可能需要想出比我在这里列出的更好的解决方案。