.nextAll()
.nextAll( [ selector ] ) 返回: jQuery
描述: 獲得每個匹配元素集合中所有下面的同輩元素,選擇性篩選的選擇器。
-
version added: 1.2.nextAll( [ selector ] )
selector選擇器字符串,用于確定到哪個前輩元素時停止匹配。
如果提供的jQuery代表了一組DOM元素,.nextAll()
方法允許我們找遍所有元素所在的DOM樹,構(gòu)建新的匹配元素的jQuery對象。
該方法選擇性地接受同一類型選擇表達(dá),我們可以傳遞給$()
函數(shù)。如果供應(yīng)選擇器參數(shù),將通過測試它們是否匹配過濾的元素。
考慮一個頁面上一個簡單的列表:
<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>
如果我們從第三個項(xiàng)目開始,我們可以找到它之后的元素:
$('li.third-item').nextAll().css('background-color', 'red');
調(diào)用后的結(jié)果是項(xiàng)目4和5變成紅色背景。 由于我們沒有提供一個選擇器表達(dá)式,祖先元素都是返回的jQuery對象的一部分。如果我們有提供一個選擇的表達(dá)式,只有在這些匹配的項(xiàng)目將包括在內(nèi)。
Examples:
Example: Locate all the divs after the first and give them a class.
<!DOCTYPE html>
<html>
<head>
<style>
div { width: 80px; height: 80px; background: #abc;
border: 2px solid black; margin: 10px; float: left; }
div.after { border-color: red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<div>first</div>
<div>sibling<div>child</div></div>
<div>sibling</div>
<div>sibling</div>
<script>$("div:first").nextAll().addClass("after");</script>
</body>
</html>
Demo:
Example: Locate all the paragraphs after the second child in the body and give them a class.
<!DOCTYPE html>
<html>
<head>
<style>
div, p { width: 60px; height: 60px; background: #abc;
border: 2px solid black; margin: 10px; float: left; }
.after { border-color: red; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>p</p>
<div>div</div>
<p>p</p>
<p>p</p>
<div>div</div>
<p>p</p>
<div>div</div>
<script>
$(":nth-child(1)").nextAll("p").addClass("after");
</script>
</body>
</html>