jQuery选择器之基本筛选选择器
2017-03-31 11:10
435 查看
筛选选择器的用法与CSS中的伪元素相似,选择器用冒号“:”开头,下面是基本选择器的描述:
$(":first"):匹配第一个元素
$(":last"):匹配最后一个元素
$(":not(selector)"):一个用来过滤的选择器,选择所有元素去除不匹配给定的选择器元素
$(":eq(index)"):在匹配的集合中选择索引值为index的元素
$(":gt(index)"):选择匹配集合中所有大于给定index(索引值)的元素
$(":even"):选择索引值为偶数的元素,从0开始计数
$(":odd"):选择索引值为奇数的元素,从0开始计数
$(":lt(index)"):选择匹配集合中所有索引值小于给定index参数的元素
$(":header"):选择所有标题元素,像h1,h2,h3等
$(":lang(language)"):选择指定语言的所有元素
$(":root"):选择该文档的根元素
$(":animated"):选择所有正在执行动画效果的元素
注意:
1. :eq(),:lt(),:get(),:even,:odd用来筛选他们前面的匹配表达式的集合元素,根据之前匹配的元素在进一步筛选,注意jQuery合集都是从0开始索引
2.:gt()是一个段落筛选,从指定索引的下一个开始,gt(1)实际上是从2开始
<!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=gbk" />
<title>筛选选择器</title>
<style type="text/css">
.left {
width: auto;
height: 120px;
}
.left div {
width: 70px;
height: 70px;
padding: 5px;
margin: 5px;
float: left;
background: #bbffaa;
border: 1px solid #ccc;
}
.bottom {
width: 800px;
}
.bottom div,
.bottom span {
display: block;
width: 80px;
height: 80px;
margin: 5px;
background: #bbffaa;
float: left;
font-size: 14px;
}
.bottom .small {
width: 60px;
height: 25px;
font-size: 12px;
background: #fab;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>基本筛选器</h2>
<h3>:first/:last/:even/:odd</h3>
<div class="left">
<div class="div">
<p>div:first</p>
<p>:even</p>
</div>
<div class="div">
<p>:odd</p>
</div>
<div class="div">
<p>:even</p>
</div>
<div class="div">
<p>:odd</p>
</div>
<div class="div">
<p>:even</p>
</div>
<div class="div">
<p>div:last</p>
<p>:odd</p>
</div>
</div>
<script type="text/javascript">
//找到第一个div
$(".div:first").css("color", "#CD00CD");
</script>
<script type="text/javascript">
//找到最后一个div
$(".div:last").css("color", "#CD00CD");
</script>
<script type="text/javascript">
//:even 选择所引值为偶数的元素,从 0 开始计数
$(".div:even").css("border", "3px groove red");
</script>
<script type="text/javascript">
//:odd 选择所引值为奇数的元素,从 0 开始计数
$(".div:odd").css("border", "3px groove blue");
</script>
<h3>:eq/:gt/:lt</h3>
<div class="left">
<div class="aaron">
<p>:lt(3)</p>
</div>
<div class="aaron">
<p>:lt(3)</p>
</div>
<div class="aaron">
<p>:eq(2)</p>
</div>
<div class="aaron">
</div>
<div class="aaron">
<p>:gt(3)</p>
</div>
<div class="aaron">
<p>:gt(3)</p>
</div>
</div>
<script type="text/javascript">
//:eq
//选着单个
$(".aaron:eq(2)").css("border", "3px groove blue");
</script>
<script type="text/javascript">
//:gt 选择匹配集合中所有索引值大于给定index参数的元素
$(".aaron:gt(3)").css("border", "3px groove blue");
</script>
<script type="text/javascript">
//:lt 选择匹配集合中所有索引值小于给定index参数的元素
//与:gt相反
$(".aaron:lt(2)").css("color", "#CD00CD");
</script>
<h3>:not</h3>
<div class="left">
<div>
<input type="checkbox" name="a" />
<p>Aaron</p>
</div>
<div>
<input type="checkbox" name="b" />
<p>慕课</p>
</div>
<div>
<input type="checkbox" name="c" checked="checked" />
<p>其他</p>
</div>
</div>
<script type="text/javascript">
//:not 选择所有元素去除不匹配给定的选择器的元素
//选取所有input中,没有checked属性的p元素,赋予颜色
$("input:not(:checked) + p").css("background-color", "#CD00CD");
</script>
</body>
</html>
$(":first"):匹配第一个元素
$(":last"):匹配最后一个元素
$(":not(selector)"):一个用来过滤的选择器,选择所有元素去除不匹配给定的选择器元素
$(":eq(index)"):在匹配的集合中选择索引值为index的元素
$(":gt(index)"):选择匹配集合中所有大于给定index(索引值)的元素
$(":even"):选择索引值为偶数的元素,从0开始计数
$(":odd"):选择索引值为奇数的元素,从0开始计数
$(":lt(index)"):选择匹配集合中所有索引值小于给定index参数的元素
$(":header"):选择所有标题元素,像h1,h2,h3等
$(":lang(language)"):选择指定语言的所有元素
$(":root"):选择该文档的根元素
$(":animated"):选择所有正在执行动画效果的元素
注意:
1. :eq(),:lt(),:get(),:even,:odd用来筛选他们前面的匹配表达式的集合元素,根据之前匹配的元素在进一步筛选,注意jQuery合集都是从0开始索引
2.:gt()是一个段落筛选,从指定索引的下一个开始,gt(1)实际上是从2开始
<!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=gbk" />
<title>筛选选择器</title>
<style type="text/css">
.left {
width: auto;
height: 120px;
}
.left div {
width: 70px;
height: 70px;
padding: 5px;
margin: 5px;
float: left;
background: #bbffaa;
border: 1px solid #ccc;
}
.bottom {
width: 800px;
}
.bottom div,
.bottom span {
display: block;
width: 80px;
height: 80px;
margin: 5px;
background: #bbffaa;
float: left;
font-size: 14px;
}
.bottom .small {
width: 60px;
height: 25px;
font-size: 12px;
background: #fab;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>基本筛选器</h2>
<h3>:first/:last/:even/:odd</h3>
<div class="left">
<div class="div">
<p>div:first</p>
<p>:even</p>
</div>
<div class="div">
<p>:odd</p>
</div>
<div class="div">
<p>:even</p>
</div>
<div class="div">
<p>:odd</p>
</div>
<div class="div">
<p>:even</p>
</div>
<div class="div">
<p>div:last</p>
<p>:odd</p>
</div>
</div>
<script type="text/javascript">
//找到第一个div
$(".div:first").css("color", "#CD00CD");
</script>
<script type="text/javascript">
//找到最后一个div
$(".div:last").css("color", "#CD00CD");
</script>
<script type="text/javascript">
//:even 选择所引值为偶数的元素,从 0 开始计数
$(".div:even").css("border", "3px groove red");
</script>
<script type="text/javascript">
//:odd 选择所引值为奇数的元素,从 0 开始计数
$(".div:odd").css("border", "3px groove blue");
</script>
<h3>:eq/:gt/:lt</h3>
<div class="left">
<div class="aaron">
<p>:lt(3)</p>
</div>
<div class="aaron">
<p>:lt(3)</p>
</div>
<div class="aaron">
<p>:eq(2)</p>
</div>
<div class="aaron">
</div>
<div class="aaron">
<p>:gt(3)</p>
</div>
<div class="aaron">
<p>:gt(3)</p>
</div>
</div>
<script type="text/javascript">
//:eq
//选着单个
$(".aaron:eq(2)").css("border", "3px groove blue");
</script>
<script type="text/javascript">
//:gt 选择匹配集合中所有索引值大于给定index参数的元素
$(".aaron:gt(3)").css("border", "3px groove blue");
</script>
<script type="text/javascript">
//:lt 选择匹配集合中所有索引值小于给定index参数的元素
//与:gt相反
$(".aaron:lt(2)").css("color", "#CD00CD");
</script>
<h3>:not</h3>
<div class="left">
<div>
<input type="checkbox" name="a" />
<p>Aaron</p>
</div>
<div>
<input type="checkbox" name="b" />
<p>慕课</p>
</div>
<div>
<input type="checkbox" name="c" checked="checked" />
<p>其他</p>
</div>
</div>
<script type="text/javascript">
//:not 选择所有元素去除不匹配给定的选择器的元素
//选取所有input中,没有checked属性的p元素,赋予颜色
$("input:not(:checked) + p").css("background-color", "#CD00CD");
</script>
</body>
</html>
相关文章推荐
- Jquery选择器的基本筛选选择器
- jQuery选择器之基本筛选选择器
- jQuery基本筛选选择器实例代码
- jQuery选择器之基本筛选选择器
- jQuery选择器之基本筛选选择器
- jQuery基本筛选选择器(二)
- jQuery选择器之基本筛选选择器
- jQuery教程06-基本筛选选择器
- JQuery选择器——基本筛选选择器和内容筛选选择器
- jQuery 选择器(基本选择器/层级选择器/过滤选择器/筛选选择器)
- jQuery基本筛选选择器(一)
- JQuery中的基本筛选选择器
- jQuery选择器之基本筛选选择器
- jQuery选择器 – 基本(Basics)
- JQuery基本选择器
- 详解强大的jQuery选择器之基本选择器、层次选择器
- jQuery基本选择器
- jQuery基本选择器
- jQuery过滤选择器-基本过滤选择器
- jQuery基础教程之强大的选择器(过滤选择器-基本过滤选择器)