常规
1、id选择器
$("#myId")
2、类选择器
$(".myclass")
3、元素选择器
$("p")
4、通配符 *
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$("*").css({ "background-color": "gray"});
//$("html").css({ "background-color": "green"});
//$(":root").css({ "background-color": "red"});
</script>
</html>
5、selector1,selector2,selectorN
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>div</div>
<p class="myClass">p class="myClass"</p>
<span>span</span>
<p class="notMyClass"> p class="notMyClass"</p>
<p class="notMyClass">p class="notMyClass" </p>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配所有满足的元素
page = {}
page.body = $("body");
page.body.find("div,span,p.myClass").css({
"background-color": "green"
});
</script>
</html>
6、div > p 找到所有 p 元素,并且这些元素都必须是 div 元素的子元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
/* div > p{
background-color:gray;
} */
</style>
</head>
<body>
<div>
<p>p1标签</p>
<p>p2标签</p>
<p>p3标签</p>
</div>
<p>div 外 p4标签</p>
</body>
<script src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
page = {}
page.body = $("body");
page.body.find("div > p").css({"background-color":"green"});
//$(document.body).css( "background", "gray" );
</script>
</html>
7、a+b 匹配所有紧跟在 a 后的b元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form>
<label>Name:</label>
<input name="name" />
<fieldset>
<label>Newsletter:</label>
<input name="newsletter" />
</fieldset>
</form>
<input name="none" />
<label>Newsletter:22</label>
<input name="newsletter222" />
<input name="newsletter333" />
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配所有紧跟在label标签后的input元素
page = {}
page.body = $("body");
page.body.find("label + input").css({"background-color":"green"});
</script>
</html>
8、a~b匹配所有与**同辈的 ** 元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<form>
<label>Name:</label>
<input name="name" />
<fieldset>
<label>Newsletter:</label>
<input name="newsletter" />
</fieldset>
</form>
<input name="none" />
<input name="none2" />
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配所有与表单同辈的 input 元素
page = {}
page.body = $("body");
page.body.find("form ~ input").css({
"background-color": "green"
});
</script>
</html>
9 :first 查找匹配的第一个元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配ul中第一个li 元素
page = {}
page.body = $("body");
page.body.find("li:first").css({ "background-color": "green"});
</script>
</html>
10 :last查找匹配的最后一个元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配ul中最后一个li 元素
page = {}
page.body = $("body");
page.body.find("li:last").css({ "background-color": "green"});
</script>
</html>
11 :not查找匹配的第一个元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<span>篮球 </span><input type="radio" name="ball" checked="checked" value="0"/>
<span>足球 </span><input type="radio" name="ball" value="1"/>
<span>足球 </span><input type="radio" name="ball" value="2"/>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
//
page = {}
page.body = $("body");
page.body.find("input:not(:checked)").prev().css({ "color": "blue"});
page.body.find("input[name='ball']").click(function(){
$(this).prev().css({ "color": ""});
page.body.find("input:not(:checked)").prev().css({ "color": "blue"});
})
</script>
</html>
12、:eq(n) 匹配ul中li下标为 n 元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li>list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配ul中li下标为 n 元素
page = {}
page.body = $("body");
page.body.find("li:eq(2)").first().css({ "background-color": "green"});
</script>
</html>
13、:odd 匹配奇数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<table>
<tr>
<td>Header 1</td>
</tr>
<tr>
<td>Value 1</td>
</tr>
<tr>
<td>Value 2</td>
</tr>
</table>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配table中奇数行
page = {}
page.body = $("body");
page.body.find("tr:odd").css({ "background-color": "green"});
</script>
</html>
14、:even 匹配偶数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<table>
<tr>
<td>Header 1</td>
</tr>
<tr>
<td>Value 1</td>
</tr>
<tr>
<td>Value 2</td>
</tr>
</table>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配table中偶数行
page = {}
page.body = $("body");
page.body.find("tr:even").css({ "background-color": "red"});
</script>
</html>
15、:gt(n) 匹配下标大于n的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<table>
<tr>
<td>Header 1</td>
</tr>
<tr>
<td>Value 1</td>
</tr>
<tr>
<td>Value 2</td>
</tr>
</table>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配tr中偶数下标大于0的行
page = {}
page.body = $("body");
page.body.find("tr:gt(0)").css({ "background-color": "red"});
</script>
</html>
16、:lt(n) 匹配tr中下标小于n的行
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<table>
<tr>
<td>Header 1</td>
</tr>
<tr>
<td>Value 1</td>
</tr>
<tr>
<td>Value 2</td>
</tr>
</table>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配tr下标小于2的tr
page = {}
page.body = $("body");
page.body.find("tr:lt(2)").css({ "background-color": "red"});
</script>
</html>
17、:header 匹配如 h1, h2, h3之类的标题元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
</style>
</head>
<body>
<div>
<h1>Header 1</h1>
<p>Contents 1</p>
<h2>Header 2</h2>
<p>Contents 2</p>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配所有h标签
page = {}
page.body = $("body");
page.body.find(":header").css({
"background-color": "red"
});
</script>
</html>15、contans 匹配包含a的元
18、:empty 匹配所有不包含子元素或者文本的空元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
table tr td {
height: 20px;
width: 60px;
background-color: aqua;
border: 1px;
}
</style>
</head>
<body>
<table>
<tr>
<td>Value 1</td>
<td></td>
</tr>
<tr>
<td>Value 2</td>
<td></td>
</tr>
</table>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配表格中所有td为空的的元素
page = {}
page.body = $("body");
page.body.find("td:empty").css({
"background-color": "white","border": "1px solid rgba(204,204,204,1)"
});
page.body.find("td:empty").append("--").css("text-align","center");
</script>
</html>
19、:parent 选取所有包含子节点或文本节点的父元素,与 :empty相反。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">查找</button>
<div class="ancestors">
<div style="width:500px;">div (曾祖父元素)
<ul>ul (祖父元素)
<li>li (父元素)
<span>span</span>
</li>
</ul>
</div>
<div style="width:500px;">div (祖父元素)
<p>p (父元素)
<span>span</span>
<span style="height: 23px;"></span>
</p>
</div>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
// 选取所有带有子元素或包含文本的 <span> 元素:
page.body.find("span:parent").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
20、:contains(text)匹配包含text的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
div {
height: 40px;
width: 100px;
background-color: aqua;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div>John Resig</div>
<div>George Martin</div>
<div>Malcom john Sinclair</div>
<div>J. Ohn</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配所有包含john的 元素 ,contains区分大小写
page = {}
page.body = $("body");
page.body.find("div:contains('john')").css({
"background-color": "green"
});
</script>
</html>
21、 :has(selector) 匹配含有选择器所匹配的元素的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
div {
height: 60px;
width: 60px;
background-color: aqua;
margin: 10px;
font-size: 15px;
}
</style>
</head>
<body>
<div>
<p>Hello</p>
</div>
<div>Hello again!
<span id="span"></span>
</div>
<div>Hello again!
<h1 class="h1"></h1>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
// 匹配含有p标签的div
page = {}
page.body = $("body");
//page.body.find("div:has(p)")
// page.body.find("div:has('#span')")
page.body.find("div:has(.h1)").css({
"background-color": "lightgray",
"font-size": "20px"
});
</script>
</html>
22 :visible 匹配所有的可见元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<span id="span1">tr总个数:</span>
<span id="span2">显示的tr个数:</span>
<table>
<tr style="display:none">
<td>tr 1</td>
</tr>
<tr>
<td>tr 2</td>
</tr>
<tr>
<td>tr 2</td>
</tr>
</table>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
//
page = {}
page.body = $("body");
page.body.find("#span1").append(page.body.find("tr").length+";")
page.body.find("#span2").append(page.body.find("tr:visible").length)
</script>
</html>
23 :hidden 匹配所有不可见元素,或者type为hidden的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<span id="span1">tr总个数:</span>
<span id="span2">隐藏的tr个数:</span>
<table>
<tr style="display:none">
<td>tr 1</td>
</tr>
<tr>
<td>tr 2</td>
</tr>
<tr>
<td>tr 3</td>
</tr>
</table>
</div>
<div class="div2">
<span id="div2_span">input总个数:</span>
<span id="div2_span2">隐藏的input个数:</span><br />
<input type="hidden" value="111"/>
<input type="text" value="222"/>
<input type="number" value="333"/>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script type="text/javascript">
//
page = {}
page.body = $("body");
page.body.find("#span1").append(page.body.find("tr").length+";")
page.body.find("#span2").append(page.body.find("tr:hidden").length)
page.body.find("#div2_span").append(page.body.find("input").length+";")
page.body.find("#div2_span2").append(page.body.find("input:hidden").length);
</script>
</html>
24 [attribute] 匹配包含给定属性的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">查找含有id的div</button>
<div class="ancestors" style="width:500px;">
<div class="cla" name="11">
<p>Hello!</p>
</div>
<div id="test2" name="12">
<p>good!</p>
</div>
<div>
<p>Hi!</p>
</div>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
//page.body.find(".ancestors").find("div[id]").css({
//page.body.find(".ancestors").find("div[id='test2']").css({
//page.body.find(".ancestors").find("div[class]").css({
//page.body.find(".ancestors").find("div[class='cla']").css({
//page.body.find(".ancestors").find("div[name']").css({
page.body.find(".ancestors").find("div[name='11']").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
25、[attribute!=value] 匹配所有不含有指定的属性,或者属性不等于特定值的元素。
等价于[:not([attr=value])]
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">查找</button>
<div class="ancestors" style="width:500px;">
<div class="cla" name="11">
<p>Hello!</p>
</div>
<div id="test2" name="12">
<p>good!</p>
</div>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("div[id !='test2']").css({
// page.body.find("div:not([id='test2'])").css({
//page.body.find(".ancestors").find("div[name !='11']").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
26、[attribute^=value] 匹配给定的属性是以某些值开始的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">匹配给定的属性是以某些值开始的元素</button>
<div class="ancestors" style="width:500px;">
<input id="input1" class="first_input" name="newsletter" placeholder="newsletter"/>
<input id="input12" class="second_input" name="milkman" placeholder="milkman"/>
<input id="input33" class="third_input" name="newsboy" placeholder="newsboy"/>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("input[id ^='input1']").css({
//page.body.find(".ancestors").find("input[name ^='news']").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
27、[attribute $= value] 匹配给定的属性是以某些值结尾的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">匹配给定的属性是以某些值结尾的元素</button>
<div class="ancestors" style="width:500px;">
<input id="input1" class="first_input" name="newsletter" placeholder="newsletter"/>
<input id="input12" class="second_input" name="milkman" placeholder="milkman"/>
<input id="input33" class="third_input" name="newsboy" placeholder="newsboy"/>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("input[class $='d_input']").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
28、[attribute*=value] 匹配给定的属性是以包含某些值的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">匹配给定的属性是以包含某些值的元素</button>
<div class="ancestors" style="width:500px;">
<input id="input1" class="first_input" name="newsletter" placeholder="newsletter"/>
<input id="input12" class="second_input" name="milkman" placeholder="milkman"/>
<input id="input33" class="third_input" name="newsboy" placeholder="newsboy"/>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
//page.body.find(".ancestors").find("input[class *='d_in']").css({
page.body.find(".ancestors").find("input[id *= 'put1']").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
29、:first-child 匹配所给选择器( :之前的选择器)的第一个子元素
:first-child选择器可以匹配多个:即为每个父级元素匹配第一个子元素。这相当于:nth-child(1)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">:first-child 匹配所给选择器( :之前的选择器)的第一个子元素</button>
<p>li:first-child</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("li:first-child").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
30、:first-of-type 匹配E的父元素的第一个E类型的孩子。等价于:nth-of-type(1) 选择器。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">点我</button>
<div class="ancestors" style="width:500px;">
<div id="n2" class="abc">
<label id="n3">label1</label>
<span id="n4">span1</span>
<span id="n5" class="abc">span2</span>
<span id="n6">span3</span>
<div id="n7">
<span id="n8" class="abc">span1</span>
<span id="n9">span2</span>
</div>
</div>
<div id="n7">
<span id="n8" class="abc">span1</span>
<span id="n9">span2</span>
</div>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("span:first-of-type").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
31、:last-child 匹配最后一个子元素(每个父元素匹配最后一个子元素)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">:last-child 匹配所给选择器( :之前的选择器)的最后一个子元素</button>
<p>li:last-child</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("li:last-child").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
32、:last-of-type 匹配E的父元素的最后一个E类型的孩子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">点我</button>
<div class="ancestors" style="width:500px;">
<div id="n2" class="abc">
<label id="n3">label1</label>
<span id="n4">span1</span>
<span id="n5" class="abc">span2</span>
<span id="n6">span3</span>
<div id="n7">
<span id="n8" class="abc">span1</span>
<span id="n9">span2</span>
</div>
</div>
<div id="n7">
<span id="n8" class="abc">span1</span>
<span id="n9">span2</span>
</div>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("span:last-of-type").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
33、:nth-child(n) 匹配所给选择器( :之前的选择器)的第n个/奇数/偶数元素(n从 1 开始)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">:nth-child(n) 匹配所给选择器( :之前的选择器)的第n个子元素</button>
<p>li:nth-child(n)</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("li:nth-child(2)").css({
//page.body.find(".ancestors").find("li:nth-child(even)").css({
//page.body.find(".ancestors").find("li:nth-child(odd)").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
34、:nth-last-child(n|even|odd|formula) 从最后一个开始查找满足条件的元素
n (从1开始)|even 偶数|odd 奇数|formula 为表达式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 10px;
}
</style>
</head>
<body>
<button id="btn">:nth-child(n) 最后一个开始查找满足条件的元素</button>
<p>li:nth-child(n)</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Kerwen</li>
<li>Linda</li>
<li>James</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
<li>Jack</li>
<li>Jackson</li>
<li>Paul</li>
<li>Thomas</li>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
//page.body.find(".ancestors").find("li:nth-last-child(2)").css({
//page.body.find(".ancestors").find("li:nth-last-child(even)").css({
//page.body.find(".ancestors").find("li:nth-last-child(odd)").css({
page.body.find(".ancestors").find("li:nth-last-child(2n+1)").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
35、:nth-of-type(n|even|odd|formula)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 10px;
}
</style>
</head>
<body>
<button id="btn">:nth-of-type </button>
<p>li:nth-of-type()</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Kerwen</li>
<li>Linda</li>
<li>James</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
<li>Jack</li>
<li>Jackson</li>
<li>Paul</li>
<li>Thomas</li>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("li:nth-of-type(2)").css({
//page.body.find(".ancestors").find("li:nth-of-type(even)").css({
//page.body.find(".ancestors").find("li:nth-of-type(odd)").css({
//page.body.find(".ancestors").find("li:nth-of-type(2n+1)").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
36、:nth-last-of-type(n|even|odd|formula)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 10px;
}
</style>
</head>
<body>
<button id="btn">:nth-last-of-type </button>
<p>li:nth-last-of-type()</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Kerwen</li>
<li>Linda</li>
<li>James</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
<li>Jack</li>
<li>Jackson</li>
<li>Paul</li>
<li>Thomas</li>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("li:nth-last-of-type(2)").css({
//page.body.find(".ancestors").find("li:nth-last-of-type(even)").css({
//page.body.find(".ancestors").find("li:nth-last-of-type(odd)").css({
//page.body.find(".ancestors").find("li:nth-last-of-type(2n+1)").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
37、:only-child 匹配在父类中唯一,没有兄弟元素的元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 10px;
}
</style>
</head>
<body>
<button id="btn">:only-child </button>
<p>li:only-child 在父类中唯一,没有兄弟元素</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Kerwen</li>
<li>Linda</li>
<li>James</li>
</ul>
<ul>
<li>Thomas</li>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("li:only-child").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
38、only-of-type 在父类中唯一,没有同类的兄弟元素
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 10px;
}
</style>
</head>
<body>
<button id="btn">:only-of-type </button>
<p>li:only-of-type 在父类中唯一,没有同类的兄弟元素</p>
<div class="ancestors" style="width:500px;">
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
<li>Kerwen</li>
<li>Linda</li>
<li>James</li>
</ul>
<ul>
<li>Thomas</li>
<span>Paul</span>
</ul>
<ul>
<span>Paul</span>
</ul>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("span:only-of-type").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
39、:enabled 匹配可用的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">enabled </button>
<p>enabled 可用</p>
<div class="ancestors" style="width:500px;">
<form>
<input name="email" disabled="disabled" />
<input name="id" />
</form>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("input:enabled").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
40、:disabled 匹配不可用的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="Icon" href="../img/favicon.ico" type="image/x-icon" />
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">disabled </button>
<p>disabled 不可用</p>
<div class="ancestors" style="width:500px;">
<form>
<input name="email" disabled="disabled" />
<input name="id" />
</form>
</div>
</body>
<script src="../js/jquery-1.7.1.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
page.body.find(".ancestors").find("input:disabled").css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>
41、 :radio :input :password :text :button :image :checked :selected
:checkbox :submit :file :enabled :disabled
42、 $.escapeSelector(selector) 匹配Id或者class中包含特殊字符的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="Icon" href="../img/favicon.ico" type="image/x-icon" />
<style>
.ancestors,
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
</head>
<body>
<button id="btn">disabled </button>
<p>disabled 不可用</p>
<div class="ancestors" style="width:500px;">
<div>
<div id="#notMe">div id="#notMe"</div>
<span id=".span"> id=".span"</span>
<div class="notMe">div class="notMe"</div>
<div class=".box myClass">div class=".box myClass"</div>
<div class=".box">span class=".box"</div>
<span class="#span"> class="#span"</span>
</div>
</div>
</body>
<script src="https://cdn.staticfile.org/jquery/3.0.0/jquery.min.js"></script>
<script>
page = {}
page.body = $("body");
page.body.find("#btn").click(function() {
//page.body.find(".ancestors").find( "#" + $.escapeSelector( "#notMe" )).css({
//page.body.find(".ancestors").find( "." + $.escapeSelector( ".box" )).css({
page.body.find(".ancestors").find("." + $.escapeSelector("#span")).css({
"color": "red",
"border": "2px solid red"
});
})
</script>
</html>