.prevAll()
.prevAll( [ selector ] ) 返回: jQuery
描述: 獲得集合中每個(gè)匹配元素的所有前面的兄弟元素,選擇性篩選的選擇器。
-
version added: 1.2.prevAll( [ selector ] )
selector一個(gè)用于匹配元素的選擇器字符串。
如果提供的jQuery代表了一組DOM元素, .prevAll() 方法允許我們能夠通過搜索DOM樹,在它們前面的元素和從匹配的元素構(gòu)造一個(gè)新的jQuery對(duì)象。
該方法選擇性地接受同一類型選擇器表達(dá)式,我們可以傳遞給$()函數(shù)。如果選擇供應(yīng),將被過濾的元素通過測(cè)試它們是否匹配。.
考慮一個(gè)頁(yè)面上有一個(gè)簡(jiǎn)單的列表:
<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>
如果我們?cè)诘谌齻€(gè)項(xiàng)目開始之前,我們可以找到它面前的元素來:
$('li.third-item').prevAll().css('background-color', 'red');
此調(diào)用的結(jié)果是項(xiàng)目1和2的背景為紅色。由于我們沒有提供一個(gè)選擇的表達(dá),這些元素被明確列入前款為對(duì)象的一部分。如果我們有提供一個(gè),這個(gè)元素將被測(cè)試的內(nèi)容在匹配之前,他們都包括在內(nèi)。
Example:
Locate all the divs preceding the last div and give them a class.
<!DOCTYPE html>
<html>
<head>
<style>
div { width:70px; height:70px; background:#abc;
border:2px solid black; margin:10px; float:left; }
div.before { border-color: red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<script>$("div:last").prevAll().addClass("before");</script>
</body>
</html>