语法说明
https://api.jquery.com/category/selectors/basic-filter-selectors/
:first
选择器:选择第一个匹配的DOM元素,例如$('div:first')
:last
选择器:选择最后一个匹配的元素,例如$('div:last')
:not()
选择器:选择所有不匹配给定选择器的元素,例如$('div:not(".one")')
:even
选择器:选择偶数索引的元素(从零开始计数)例如$('div:even')
:odd
选择器:选择奇数索引的元素(从零开始计数),例如$('div:odd')
:eq()
选择器:选择匹配集中索引为n的元素,例如$('div:eq(3)')
:gt()
选择器:选择匹配集中索引大于index的所有元素,例如$('div:gt(3)')
:lt()
选择器:选择匹配集中索引小于index的所有元素,例如$('div:lt(3)')
:header
选择器:选择所有标题元素,如h1、h2、h3等,例如$(':header')
:animated
选择器:选择在运行选择器时处于动画过程中的所有元素。
示例
:first
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变第一个div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:first').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:last
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变最后一个div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:last').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:not()
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变class不为one的所有div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:not(".one")').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:even
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变索引值为偶数的div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:even').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:odd
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变索引值为奇数的div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:odd').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:gt()
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变索引值大于3的div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:gt(3)').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:eq()
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变索引值等于3的div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:eq(3)').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:lt()
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变索引值小于3的div元素的背景色为红色"><br><br>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$('div:lt(3)').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
:header
选择器
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-3.7.1.js"></script>
<style>
body {
font-size: 24px;
}
#one {
float: left;
width: 300px;
height: 300px;
background-color: #979ccd;
}
#two {
margin-left: 20px;
float: left;
width: 300px;
height: 300px;
background-color: skyblue;
}
.mini {
margin-top: 10px;
width: 100px;
height: 100px;
background-color: blueviolet;
}
.one {
float: left;
margin-left: 20px;
width: 300px;
height: 300px;
background-color: #979ccd;
}
span {
display: inline-block;
margin-left: 20px;
width: 200px;
height: 200px;
background-color: purple;
}
input {
font-size: 30px;
margin-bottom: 30px;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,改变所有的标题元素的背景色为红色"><br><br>
<h1>h1的内容:千里之行,始于足下</h1>
<h3>h3的内容:中秋节、国庆节的脚步越来越近了</h3>
<div id="one">div:id为one</div>
<div id="two" title="test">div:id为two,title="test"
<div class="mini">div:class为mini</div>
</div>
<div class="one" title="test02">div:class是one,title="test02"
<div class="mini">div:class是mini</div>
<div class="mini">div:class是mini</div>
</div>
<div class="one">div:class是one</div>
<span>span</span>
<script>
$('#b1').click(function () {
$(':header').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮: