好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

CSS3选择器属性选择器

上一节在《CSS3选择器——基本选择器》中主要介绍了CSS3选择器的第一部分,这节主要和大家一起来学习CSS3选择器的第二部分——属性选择器。属性选择器早在CSS2中就被引入了,其主要作用就是 对带有指定属性的HTML 元素设置样式 。使用CSS3属性选择器,你可以只指定元素的某个属性,或者你还可以同时指定元素的某个属性和其对应的属性值。

从上一节展示的CSS3选择器的图表中,我们可以知道,CSS3的属性选择器主要包括以下几种:

E[attr] :只使用属性名,但没有确定任何属性值; E[attr="value"] :指定属性名,并指定了该属性的属性值; E[attr~="value"] :指定属性名,并且具有属性值,此属性值是一个词列表,并且以空格隔开,其中词列表中包含了一个value词,而且等号前面的[〜]不能不写; E[attr^="value"] :指定了属性名,并且有属性值,属性值是以value开头的; E[attr$="value"] :指定了属性名,并且有属性值,而且属性值是以value结束的; E[attr*="value"] :指定了属性名,并且有属性值,而且属值中包含了value; E[attr|="value"] :指定了属性名,并且属性值是value或者以[value-]开头的值(比如说zh-cn);

为了更好的说明CSS3属性选择器的使用方法,我们把第一节的demo换成别的结构,如下所示:

   div   class  ="demo clearfix"  > 
       a   href  ="http://www.w3cplus.com"   target  ="_blank"   class  ="links item first"   id  ="first"   title  ="w3cplus"  > 1   a  > 
       a   href  =""   class  ="links active item"   title  ="test website"   target  ="_blank"   lang  ="zh"  > 2   a  > 
       a   href  ="sites/file/test.html"   class  ="links item"   title  ="this is a link"   lang  ="zh-cn"  > 3   a  > 
       a   href  ="sites/file/test.png"   class  ="links item"   target  ="_balnk"   lang  ="zh-tw"  > 4   a  > 
       a   href  ="sites/file/image.jpg"   class  ="links item"   title  ="zh-cn"  > 5   a  > 
       a   href  ="mailto:w3cplus@hotmail"   class  ="links item"   title  ="website link"   lang  ="zh"  > 6   a  > 
       a   href  =""   class  ="links item"   title  ="open the website"   lang  ="cn"  > 7   a  > 
       a   href  =""   class  ="links item"   title  ="close the website"   lang  ="en-zh"  > 8   a  > 
       a   href  =""   class  ="links item"   title  ="http://www.sina.com"  > 9   a  > 
       a   href  =""   class  ="links item last"   id  ="last"  > 10   a  > 
   div  >  

查看更多关于CSS3选择器属性选择器的详细内容...

  阅读:36次