css选择器----属性选择器

3、属性选择器

(1)格式:E[attr] E标签中带有attr属性
例:<body>
<div class="pic_box"> 
	<img src="images/bg1.jpg">
    <div class="nav"> 
        <a href="#1" class="links item first" title="w3cplus" target="_blank" id="first" >1</a> 
        <a href="#2" class="links active item" title="test website" target="_blank" lang="zh">2</a> 
        <a href="#3" class="links item" title="this is a link" lang="zh-cn">3</a> 
        <a href="#4" class="links item" target="_balnk" lang="zh-tw">4</a> 
        <a href="#5" class="links item" title="zh-cn">5</a> 
        <a href="#6" class="links item" title="website link" lang="zh">6</a> 
        <a href="#7" class="links item" title="open the website" lang="cn">7</a> 
        <a href="#8" class="links item" title="close the website" lang="en-zh">8</a> 
        <a href="#9" class="links item" title="http://www.baidu.com">9</a> 
        <a href="#10" class="links item last" id="last">10</a> 
    </div>
</div>
</body>

如样式为:.nav a[id] {background: blue; color:yellow;font-weight:bold;}
则:

在这里插入图片描述

如图所示:.nav a[id] 只对a标签中具有id属性的对象使用了新样式
(1)格式:E[attr=“value”] E标签中attr属性的值为value
例:<body>
<div class="pic_box"> 
	<img src="images/bg1.jpg">
    <div class="nav"> 
        <a href="#1" class="links item first" title="w3cplus" target="_blank" id="first" >1</a> 
        <a href="#2" class="links active item" title="test website" target="_blank" lang="zh">2</a> 
        <a href="#3" class="links item" title="this is a link" lang="zh-cn">3</a> 
        <a href="#4" class="links item" target="_balnk" lang="zh-tw">4</a> 
        <a href="#5" class="links item" title="zh-cn">5</a> 
        <a href="#6" class="links item" title="website link" lang="zh">6</a> 
        <a href="#7" class="links item" title="open the website" lang="cn">7</a> 
        <a href="#8" class="links item" title="close the website" lang="en-zh">8</a> 
        <a href="#9" class="links item" title="http://www.baidu.com">9</a> 
        <a href="#10" class="links item last" id="last">10</a> 
    </div>
</div>
</body>

如样式为:.nav a[id="first"] {background: blue; color:yellow;font-weight:bold;}
则:

在这里插入图片描述

如图所示:.nav a[id=“first”] 只对a标签中id属性等于first的值使用了新样式
(3)格式:E[attr~=value] E标签中attr属性的值包含value
例:<body>
<div class="pic_box"> 
	<img src="images/bg1.jpg">
    <div class="nav"> 
        <a href="#1" class="links item first" title="w3cplus" target="_blank" id="first" >1</a> 
        <a href="#2" class="links active item" title="test website" target="_blank" lang="zh">2</a> 
        <a href="#3" class="links item" title="this is a link" lang="zh-cn">3</a> 
        <a href="#4" class="links item" target="_balnk" lang="zh-tw">4</a> 
        <a href="#5" class="links item" title="zh-cn">5</a> 
        <a href="#6" class="links item" title="website link" lang="zh">6</a> 
        <a href="#7" class="links item" title="open the website" lang="cn">7</a> 
        <a href="#8" class="links item" title="close the website" lang="en-zh">8</a> 
        <a href="#9" class="links item" title="http://www.baidu.com">9</a> 
        <a href="#10" class="links item last" id="last">10</a> 
    </div>
</div>
</body>

如样式为:.nav a[title~="website"]{background:orange;color:green;}
则:

在这里插入图片描述

如图所示:.nav a[title~=“website”] 对a标签中title属性值中包含了website的所有对象使用了新样式
(4)格式:E[attr^=value] E标签中attr属性以value开头的值
  例:<body>
<div class="pic_box"> 
	<img src="images/bg1.jpg">
    <div class="nav"> 
        <a href="#1" class="links item first" title="w3cplus" target="_blank" id="first" >1</a> 
        <a href="#2" class="links active item" title="test website" target="_blank" lang="zh">2</a> 
        <a href="#3" class="links item" title="this is a link" lang="zh-cn">3</a> 
        <a href="#4" class="links item" target="_balnk" lang="zh-tw">4</a> 
        <a href="#5" class="links item" title="zh-cn">5</a> 
        <a href="#6" class="links item" title="website link" lang="zh">6</a> 
        <a href="#7" class="links item" title="open the website" lang="cn">7</a> 
        <a href="#8" class="links item" title="close the website" lang="en-zh">8</a> 
        <a href="#9" class="links item" title="http://www.baidu.com">9</a> 
        <a href="#10" class="links item last" id="last">10</a> 
    </div>
</div>
</body>

如样式为:.nav a[title^="http://"]{background:orange;color:green;}
则:

在这里插入图片描述

如图所示:.nav a[title^=“http://”] 对a标签中title属性值以http://开头的对象使用了新样式
(5)格式:E[attr$=value] E标签中attr属性以value结尾的值
 例:<body>
<div class="pic_box"> 
	<img src="images/bg1.jpg">
    <div class="nav"> 
        <a href="#1" class="links item first" title="w3cplus" target="_blank" id="first" >1</a> 
        <a href="#2" class="links active item" title="test website" target="_blank" lang="zh">2</a> 
        <a href="#3" class="links item" title="this is a link" lang="zh-cn">3</a> 
        <a href="#4" class="links item" target="_balnk" lang="zh-tw">4</a> 
        <a href="#5" class="links item" title="zh-cn">5</a> 
        <a href="#6" class="links item" title="website link" lang="zh">6</a> 
        <a href="#7" class="links item" title="open the website" lang="cn">7</a> 
        <a href="#8" class="links item" title="close the website" lang="en-zh">8</a> 
        <a href="#9" class="links item" title="http://www.baidu.com">9</a> 
        <a href="#10" class="links item last" id="last">10</a> 
    </div>
</div>
</body>

如样式为: .nav a[title$="site"]{background:black;color:white;}
则:

在这里插入图片描述

如图所示: .nav a[title$=“site”] 对a标签中title属性值以site结尾的对象使用了新样式
(6)格式:E[attr*=value] E标签中attr属性的值包含value (作用相当于:E[attr~=value])
(7)格式:E[attr|=value] E标签中attr属性的值以value-开头或者仅为value
   例:<body>
<div class="pic_box"> 
	<img src="images/bg1.jpg">
    <div class="nav"> 
        <a href="#1" class="links item first" title="w3cplus" target="_blank" id="first" >1</a> 
        <a href="#2" class="links active item" title="test website" target="_blank" lang="zh">2</a> 
        <a href="#3" class="links item" title="this is a link" lang="zh-cn">3</a> 
        <a href="#4" class="links item" target="_balnk" lang="zh-tw">4</a> 
        <a href="#5" class="links item" title="zh-cn">5</a> 
        <a href="#6" class="links item" title="website link" lang="zh">6</a> 
        <a href="#7" class="links item" title="open the website" lang="cn">7</a> 
        <a href="#8" class="links item" title="close the website" lang="en-zh">8</a> 
        <a href="#9" class="links item" title="http://www.baidu.com">9</a> 
        <a href="#10" class="links item last" id="last">10</a> 
    </div>
</div>
</body>   

如样式为:.nav a[lang|="zh"]{background:gray;color:yellow;}
则:

在这里插入图片描述

如图所示: .nav a[lang|=“zh”] 对a标签中lang属性值以zh-开头或者仅为zh

的对象使用了新样式

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值