在CSS3属性选择器中,E[attr*=val]
和E[attr~=val]
是很容易混淆的,举个栗子:
<style type="text/css">
.demo{
width: 300px;
border:1px solid #ccc;
padding: 10px;
overflow: hidden;
margin: 20 auto;
}
.demo a {
float: left;
display: inline-block;height: 50px;
line-height: 50px;
width: 50px;
border-radius: 10px;
text-align: center;
background: #aac;
color: blue;
font: bold 20/50px arial;
margin-right:5px;
text-decoration: none;
margin: 5px;
}
</style>
<div class="demo">
<a class="links item" title="website link">1</a>
<a class="links item" title="open the website">2</a>
<a class="links item" title="close the website">3</a>
<a class="linksitem last" title="websiteitem link">4</a>
</div>
使用E[attr*=val]
,即:a[class*=links]{background: green;}
,此时,我们是给所有class不管是“links”,还是“linksitem”,标签a1、a2、a3、a4背景都变成绿色了,如图:
然后,我们来使用E[attr~=val]
吧!!即:a[title ~=website]{background: yellow;}
,我们来观察,发现…直接上图吧!:
发现有木有前三个标签都变成黄色,只有最后一个没有变黄,我们上去看一下第四个标签的title代码:title="websiteitem link"
,原来第4个的title是websiteitem
总结:
他们的区别是:E[attr*=val]匹配的是元素属性值中只要包含val字符串就可以了,而E[attr~=val]匹配的是元素属性值中要包含“val”这个值,而不是在字符串中包含val。