.find()

.find( selector )返回类型:jQuery

描述:取得匹配的元素的当前集合中每个元素的后代,用一个选择器、jQuery对象或元素来筛选。

给定一个jQuery对象,代表一些DOM元素集合,.find()方法允许我们在DOM树中搜索遍这些元素的后代元素,并根据匹配的元素构造一个新jQuery对象。.find()方法和.children()方法很相似,但是后者只能遍历DOM树中下面一个级别。

针对.find()方法的第一个签名接受了一个选择器表达式,与可以传递给%()函数的选择器表达式类型相同。必须通过测试来筛选元素,测试它们是否匹配此选择器。此表达 允许包含选择器,譬如> p ,它将找到所有的作为jQuery对象中某元素的子元素的段落。

请考虑一个网页,上面有基本的嵌套列表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<ul class="level-1">
<li class="item-i">I</li>
<li class="item-ii">II
<ul class="level-2">
<li class="item-a">A</li>
<li class="item-b">B
<ul class="level-3">
<li class="item-1">1</li>
<li class="item-2">2</li>
<li class="item-3">3</li>
</ul>
</li>
<li class="item-c">C</li>
</ul>
</li>
<li class="item-iii">III</li>
</ul>

如果从项目2开始,我们可以找到它内部的列表项:

1
$( "li.item-ii" ).find( "li" ).css( "background-color", "red" );

这次调用的结果是项目A、B、1、2、3和C有了红色背景。哪怕项目2匹配选择器表达式,它也不包括在结果中;只有后代被考虑为用来匹配的候选项。

与大多数树遍历方法不一样的是,调用.find()必须要用选择器表达式。如果我们需要检索所有的后代元素,我们可以传递通用选择器'*'来实现这。

选择器上下文是用.find()方法实现的;因此, $( "li.item-ii" ).find( "li" )等同于$( "li", "li.item-ii" )

自从jQuery 1.6以来,我们还可以用一个给定的jQuery集合或元素来筛选选区。利用与上面相同的嵌套列表,如果我们开始于:

1
var allListElements = $( "li" );

然后传入要查找的jQuery对象:

1
$( "li.item-ii" ).find( allListElements );

这将返回一个jQuery集合,它包含了作为项目2的后代的列表元素。

类似地,可以传入一个要查找的元素。

1
2
var item1 = $( "li.item-1" )[ 0 ];
$( "li.item-ii" ).find( item1 ).css( "background-color", "red" );

此调用的结果将是项目1有了红色背景。

示例:

从所有的段落开始,搜索后代<span>元素,与$( "p span" )相同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>find demo</title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p><span>Hello</span>, how are you?</p>
<p>Me? I'm <span>good</span>.</p>
<script>
$( "p" ).find( "span" ).css( "color", "red" );
</script>
</body>
</html>

演示:

一个选区,使用所有<span>标签的jQuery集合。只有<p>标签内部的<span>变成了红色,与此同时,其它依然是蓝色的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>find demo</title>
<style>
span {
color: blue;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p><span>Hello</span>, how are you?</p>
<p>Me? I'm <span>good</span>.</p>
<div>Did you <span>eat</span> yet?</div>
<script>
var spans = $( "span" );
$( "p" ).find( spans ).css( "color", "red" );
</script>
</body>
</html>

演示:

给每个单词包围<span>,然后加上悬停,给带有字母t的单词加斜体。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>find demo</title>
<style>
p {
font-size: 20px;
width: 200px;
color: blue;
font-weight: bold;
margin: 0 10px;
}
.hilite {
background: yellow;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<p>
When the day is short
find that which matters to you
or stop believing
</p>
<script>
var newText = $( "p" ).text().split( " " ).join( "</span> <span>" );
newText = "<span>" + newText + "</span>";
$( "p" )
.html( newText )
.find( "span" )
.hover(function() {
$( this ).addClass( "hilite" );
}, function() {
$( this ).removeClass( "hilite" );
})
.end()
.find( ":contains('t')" )
.css({
"font-style": "italic",
"font-weight": "bolder"
});
</script>
</body>
</html>

演示: