CSS属性选择器不能用于href

Igo*_*kov 97 html css css-selectors css3

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

我有这个HTML:

<a href="/manual.pdf">A PDF File</a>
Run Code Online (Sandbox Code Playgroud)

这个css:

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

为什么背景不是红色?

Boo*_*eus 185

你的href之后使用$.这将使属性值与字符串的结尾匹配.

a[href$='.pdf'] { /*css*/ }
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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

  • 这个答案对选择者的解释比w3schools更好. (6认同)