可见性过滤选择器是根据元素的可见和不可见状态来选择相应的元素。
在可见性选择器中,需要注意选择器:hidden,它不仅包括样式属性display为“none”的元素,也包括文本隐藏域(<input type="hidden"/>)和Visibility:hidden之类的元素。
属性过滤选择器的过滤规则是通过元素的属性来获取相应的元素。
代码示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>可见性和属性滤选择器</title>
<script src="../libs/jquery.js"></script>
<style type="text/css">
div,span,p {
width: 140px;
height: 140px;
margin: 5px;
background: #aaa;
border: #000 1px solid;
float: left;
font-size: 17px;
font-family: Verdana;
}
div.mini {
width: 55px;
height: 55px;
background-color: #aaaaaa;
font-size: 12px;
}
div.hide{
display: none;
}
</style>
</head>
<body>
<div class="one" id="one">
id is one,class also is one
<div class="mini">class name is mini</div>
</div>
<div class="one" id="two" title="test">
id is two, class is one, title is test
<div class="mini" title="other">class is mini,title is other</div>
<div class="mini" title="test">class is mini, title is test</div>
</div>
<div class="one">
<div class="mini">class is mini</div>
<div class="mini">class is mini</div>
<div class="mini">class is mini</div>
<div class="mini"></div>
</div>
<div class="one">
<div class="mini">class is mini</div>
<div class="mini">class is mini</div>
<div class="mini">class is mini</div>
<div class="mini" title="test">class is mini, title is test</div>
</div>
<div style="display: none" class="none">
style的display为“none”的div
</div>
<div class="hide"> class is hide</div>
<div>
包含input的type为“hidden”的div
<input type="hidden" size="8">
</div>
<span id="mover">正在执行动画的sapn元素</span>
<script>
//可见性过滤选择器
/*$("div:visible")//改变所有可见的div元素的背景色
.css("background","#339966");*/
/*$("div:hidden").show(3000);//显示隐藏的div元素,3000为隐藏元素显示出来的速度,3000是时间,单位是毫秒可任意设置*/
//属性过滤选择器
/*$("div[title]")//改变含有属性title的div元素的背景色
.css("background","#660000");*/
/*$("div[title=test]")//改变属性title值等于test的div元素的背景色
.css("background","#666699");*/
/*$("div[title!=test]")//改变属性title值不等于test的div元素背景色
.css("background","#660099");*/
/*$("div[title^=te]")//改变属性title值以te开始的div元素的背景色
.css("background","#669933");*/
/*$("div[title$=est]")//改变属性title值以est结束的div元素的背景色
.css("background","#66FF33");*/
/*$("div[title*=es]")//改变属性title值含有es的div元素的背景色
.css("background","#9900CC");*/
$("div[id][title*=es]")//改变含有属性id,并且属性title值含有es的div元素的背景色
.css("background","#996600")
</script>
</body>
</html>