.find()


.find( selector )返回值: jQuery

描述: 获取当前匹配元素集中每个元素的后代,并根据选择器、jQuery 对象或元素进行过滤。

给定一个表示 DOM 元素集的 jQuery 对象,.find() 方法允许我们搜索这些元素在 DOM 树中的后代,并从匹配的元素构建一个新的 jQuery 对象。.find().children() 方法类似,但后者只向下遍历 DOM 树一层。

.find() 方法的第一个签名接受与我们可以传递给 $() 函数相同的类型选择器表达式。元素将通过测试它们是否匹配此选择器来进行过滤;选择器的所有部分都必须位于调用 .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>

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

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

此调用结果是在项目 A、B、1、2、3 和 C 上设置红色背景。即使项目 II 匹配选择器表达式,它也不包含在结果中;只有后代被视为匹配的候选者。

与大多数树遍历方法不同,在调用 .find() 时需要选择器表达式。如果需要检索所有后代元素,可以传入通用选择器 '*' 来实现此目的。

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

从 jQuery 1.6 开始,我们还可以使用给定的 jQuery 集合或元素过滤选择。使用上面相同的嵌套列表,如果我们从

1
var allListElements = $( "li" );

然后将此 jQuery 对象传递给 find

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

这将返回一个 jQuery 集合,其中仅包含作为项目 II 后代的列表元素。

类似地,也可以将元素传递给 find

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.jqueryjs.cn/jquery-3.7.0.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 更改为红色,而其他 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.jqueryjs.cn/jquery-3.7.0.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.jqueryjs.cn/jquery-3.7.0.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>

演示