html - CSS 属性选择器对 href 不起作用

标签 html css css-selectors

我需要在 css 中使用属性选择器来更改不同颜色和图像的链接,但它不起作用。

我有这个 html:

<a href="/manual.pdf">A PDF File</a>

还有这个 CSS:

a {
     display: block;
     height: 25px;
     padding-left: 25px;
     color:#333;
     font: bold 15px Tahoma;
     text-decoration: none;
 }
 a[href='.pdf'] { background: red; }

为什么背景不是红色的?

最佳答案

在您的 href 之后使用 $。这将使属性值匹配字符串的结尾。

a[href$='.pdf'] { /*css*/ }

JSFiddle:http://jsfiddle.net/UG9ud/

E[foo]        an E element with a "foo" attribute (CSS 2)
E[foo="bar"]  an E element whose "foo" attribute value is exactly equal to "bar" (CSS 2)
E[foo~="bar"] an E element whose "foo" attribute value is a list of whitespace-separated values, one of which is exactly equal to "bar" (CSS 2)
E[foo^="bar"] an E element whose "foo" attribute value begins exactly with the string "bar" (CSS 3)
E[foo$="bar"] an E element whose "foo" attribute value ends exactly with the string "bar" (CSS 3)
E[foo*="bar"] an E element whose "foo" attribute value contains the substring "bar" (CSS 3)
E[foo|="en"]  an E element whose "foo" attribute has a hyphen-separated list of values beginning (from the left) with "en" (CSS 2)

来源:http://www.w3.org/TR/selectors/

关于html - CSS 属性选择器对 href 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58721168/

相关文章:

html - z-index 不适用于自定义嵌套工具提示

javascript - 如何居中并保持对模态图像的关注

html - 旋转文本的 CSS 定位和对齐

html - 将一组 CSS 表格单元格换行到新行

css - css 中的first-of-type 效果太好了吗?

html - 如何只在第一个类型元素中添加内容?

javascript - 在对等 AngularJS Controller 中重构一些重复代码以管理 $intervals 的想法

javascript - 如何为随机 HTML 设置深色模式?

html - 如何加长滚动效果中允许的文字?

CSS 输入[类* ="span"]