考虑以下代码:
a:before {
content: "(" attr(hre)")";
color: red;
}<a>First line <br></a>
<a hre="No Line"> This is styled with attr() function </a>
当我们运行脚本时,没有一行出现在第二个anchor元素之前。但是括号出现在两个anchor元素之前。
attr()函数是否只工作在那些具有该特定属性的元素上?attr()是这样定义的吗?
发布于 2016-02-29 11:07:27
attr()函数是否只工作在那些具有该特定属性的元素上?attr()是这样定义的吗?
是。当attr()引用的属性从元素中丢失时,attr()返回一个空字符串。它不影响您可能为content属性指定的其他字符串,这就是为什么两个元素的括号都出现的原因。
发布于 2016-02-29 10:59:22
你可以用属性选择器来做这件事
a[hre]:before {
content: "(" attr(hre)")";
color: red;
}<a>First line <br></a>
<a hre="No Line"> This is styled with attr() function </a>
https://stackoverflow.com/questions/35695886
复制相似问题