.prevAll()


.prevAll( [selector ] )傳回: jQuery

說明: 取得相符元素集合中每個元素的所有前置兄弟元素,可選擇透過選擇器過濾,並以反向文件順序排列。

假設有一個表示 DOM 元素集合的 jQuery 物件,.prevAll() 方法會搜尋 DOM 樹中這些元素的前置元素,並從相符元素建構一個新的 jQuery 物件;元素會以最接近的兄弟元素開始,依序傳回。

此方法選擇性地接受與我們傳遞給 $() 函式的相同類型的選取器表達式。如果提供選取器,將透過測試元素是否與其相符來篩選元素。

考慮一個包含簡單清單的頁面

1
2
3
4
5
6
7
<ul>
<li>list item 1</li>
<li>list item 2</li>
<li class="third-item">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>

如果我們從第三個項目開始,我們可以找到出現在它之前的元素

1
$( "li.third-item" ).prevAll().css( "background-color", "red" );

此呼叫的結果是在項目 1 和 2 後面加上紅色背景。由於我們沒有提供選取器表達式,因此這些前置元素明確包含在物件中。如果我們提供一個,則會在包含元素之前測試元素是否相符。

注意:許多 API,例如 appendwrapAll 會按照它們出現在 jQuery 物件中的順序處理節點。這可能會對使用反向文件順序的 API(例如 .prevAll())造成問題。考慮以下範例

1
2
3
4
5
<div>
<div>First</div>
<div>Second</div>
<div class="last-item">Last</div>
</div>

以下呼叫

1
2
3
$( ".last-item" )
.prevAll()
.wrapAll( "<div class='wrapper'></div>" );

將產生以下 HTML

1
2
3
4
5
6
7
<div>
<div class="wrapper">
<div>Second</div>
<div>First</div>
</div>
<div class="last-item">Last</div>
</div>

因為「項目 2」首先附加到包裝器 div。若要解決此問題,您可以在 .prevAll() 輸出上先使用 .uniqueSort()

1
2
3
4
$( ".last-item" )
.prevAll()
.uniqueSort()
.wrapAll( "<div class='wrapper'></div>" );

請注意,.uniqueSort() 方法僅在 jQuery 3.7.0 或更新版本中可用。在較舊版本中,您需要使用 $.uniqueSort() 來達到類似的效果

1
2
3
var prevSiblings = $( ".last-item" ).prevAll();
$.uniqueSort( prevSiblings );
prevSiblings.wrapAll( "<div class='wrapper'></div>" );

範例

找出所有位於最後一個 div 之前的 div,並給它們一個類別。

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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>prevAll demo</title>
<style>
div {
width: 70px;
height: 70px;
background: #abc;
border: 2px solid black;
margin: 10px;
float: left;
}
div.before {
border-color: red;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<script>
$( "div" ).last().prevAll().addClass( "before" );
</script>
</body>
</html>

示範

找出所有位於最後一個項目之前的 div,並使用類別 wrapper 包裝它們 - 使用或不使用 .uniqueSort()

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>prevAll demo</title>
<style>
body {
display: flex;
}
.container {
display: flex;
margin: 10px 50px 10px 10px;
}
.wrapper {
position: relative;
display: flex;
padding: 30px 10px 10px 10px;
background: #def;
border: 2px solid black;
}
.wrapper::before {
content: attr(data-content);
position: absolute;
top: 15px;
left: 15px;
}
.item {
display: flex;
justify-content: center;
align-items: center;
width: 70px;
height: 70px;
background: #abc;
border: 2px solid black;
margin: 10px;
font-size: 50px;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.0.js"></script>
</head>
<body>
<div class="container" id="container-1">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
<div class="container" id="container-2">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
<script>
$( "#container-1" )
.find( ".item" )
.last()
.prevAll()
.wrapAll( "<div class='wrapper' data-content='No uniqueSort'></div>" );
$( "#container-2" )
.find( ".item" )
.last()
.prevAll()
.uniqueSort()
.wrapAll( "<div class='wrapper' data-content='With uniqueSort'></div>" );
</script>
</body>
</html>

示範