.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.jquery.com/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 會變為紅色,而其他標籤則保持藍色。

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-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.jquery.com/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>

示範