语法说明
https://api.jquery.com/category/selectors/attribute-selectors/
- 带有属性选择器:选择具有指定属性且其值任意的元素,
$('A[属性名]')
,如$( "div[id]" )
- 属性等于选择器:选择具有指定属性且其值恰好等于某个特定值的元素,
$('A[属性名=属性值]')
,如$( "input[value='Hot Fuzz']" )
- 属性不等于选择器:选择那些没有指定属性,或者有指定属性但其值不等于某个特定值的元素,
$('A[属性名!=属性值]')
,如$( "input[name!='newsletter']" )
- 属性开头选择器:选择具有指定属性且其值恰好以给定字符串开头的元素,
$('A[属性名^=属性值]')
,如$( "input[name^='news']" )
- 属性结尾选择器:选择具有指定属性且其值恰好以给定字符串结尾的元素。比较是区分大小写的。
$('A[属性名$=属性值]')
,如$( "input[name$='letter']" )
- 属性包含选择器:选择具有指定属性且其值包含给定子字符串的元素。
$('A[属性名*=属性值]')
,如$( "input[name*='man']" )
- 多个属性选择器:匹配所有指定的属性过滤器的元素。
$('A[属性名=值]...[属性名=值]')
,如$( "input[id][name$='man']" )
示例
带有属性选择器
<!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>
#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;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,将含有属性title的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[title]').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
属性等于选择器
<!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>
#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;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,将属性title值等于test的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[title="test"]').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
属性不等于选择器
<!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>
#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;
}
</style>
</head>
<body>
<input type="button" id="b1" value="点击按钮,将属性title值不等于test的div元素(没有属性title的也将被选中)背景色改为红色"><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[title!="test"]').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
属性开头选择器
<!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="点击按钮,属性title值以te开始的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[title^="te"]').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
属性结尾选择器
<!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="点击按钮,属性title值以est结束的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[title$="est"]').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
属性包含选择器
<!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="点击按钮,属性title值含有es的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[title*="es"]').css('background-color', 'red')
})
</script>
</body>
</html>
点击上面按钮:
多个属性选择器
<!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="点击按钮,选取有属性id的元素,然后在结果中选取属性title值含有es的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[id][title*="es"]').css('background-color', 'red')
})
</script>
</body>
</html>