CSS3属性选择器之:教你区分E[attr*=val]和E[attr~=val]的使用方法!!

686 阅读2分钟

在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[title ~=website]{background: yellow;},我们来观察,发现...直接上图吧!:

咦....!!发现有木有前三个标签都变成黄色,只有最后一个没有变黄,我们上去看一下第四个标签的title代码:title="websiteitem link",原来第4个的title"websiteitem,所以我们来总结一下E[attr*=val]E[attr~=val]的区别:
他们的区别是:E[attr*=val]匹配的是元素属性值中只要包含val字符串就可以了,而E[attr~=val]匹配的是元素属性值中要包含“val”,并不仅是字符串。例如a[title~=website]属性中的website是一个单词,而a[title*=links]中的links不一定是一个单词,就如上面的示例中,可以是“linksitem”。