通过CSS显示图像而不是url

通过CSS显示图像而不是url,css,attr,Css,Attr,我有: 我可以做到: a{ 内容:url(/media/media/about logo.png); } 我需要: a{ 内容:url(attr(href)); } 这是行不通的 我需要css将href中的文本作为url使用,而不需要硬编码。纯css和HTML无法实现这一点。您需要使用JavaScript。为了简单起见,我在这里使用jQuery $("a").each(function () { $(this).css('content', 'url('+$(this).attr('hr

我有:

我可以做到:

a{
内容:url(/media/media/about logo.png);
}

我需要:

a{
内容:url(attr(href));
}

这是行不通的


我需要css将href中的文本作为url使用,而不需要硬编码。

纯css和HTML无法实现这一点。您需要使用JavaScript。为了简单起见,我在这里使用jQuery

$("a").each(function () {
   $(this).css('content', 'url('+$(this).attr('href')+')');
});
不过,此时,您最好只使用
.text
,这将产生相同的结果

$("a").each(function () {
   $(this).text($(this).attr('href'));
});

使用CSS没有可行的方法来实现这一点。也许是SASS,但只能通过动态黑客/编译。