Get a set of elements containing the unique ancestors of the matched set of elements (except for the root element). The matched elements can be filtered with an optional expression.
返回值jQuery
参数expr (String) : (可选) 用于筛选祖先元素的表达式
示例找到每个span元素的所有祖先元素。
HTML 代码:
<html><body><div><p><span>Hello</span></p><span>Hello Again</span></div></body></html>
jQuery 代码:
$("span").parents()
找到每个span的所有是p元素的祖先元素。
jQuery 代码:
$("span").parents("p")
----------------------------------------------------------------------------------------------------------------
prev([expr])
取得一个包含匹配的元素集合中每一个元素紧邻的前一个同辈元素的元素集合。
可以用一个可选的表达式进行筛选。只有紧邻的同辈元素会被匹配到,而不是前面所有的同辈元素。
Get a set of elements containing the unique previous siblings of each of the matched set of elements.
Use an optional expression to filter the matched set. Only the immediately previous sibling is returned, not all previous siblings.
返回值jQuery
参数expr (String) : (可选) 用于筛选前一个同辈元素的表达式
示例找到每个段落紧邻的前一个同辈元素。
HTML 代码:
<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
jQuery 代码:
$("p").prev()
结果:
[ <div><span>Hello Again</span></div> ]
找到每个段落紧邻的前一个同辈元素中类名为selected的元素。
HTML 代码:
<div><span>Hello</span></div><p>Hello Again</p><p>And Again</p>
jQuery 代码:
$("p").prev(".selected")
结果:
[ <p>Hello Again</p> ]
----------------------------------------------------------------------------------------------------------------
prevAll([expr])
查找当前元素之前所有的同辈元素
可以用表达式过滤。
Find all sibling elements before the current element.
Use an optional expression to filter the matched set.
返回值jQuery
参数expr (String) : (可选) 用于过滤的表达式
示例给最后一个之前的所有div加上一个类
HTML 代码:
<div></div><div></div><div></div><div></div>
jQuery 代码:
$("div:last").prevAll().addClass("before");
结果:
[ <div></div>, <div></div>, <div></div> ]
----------------------------------------------------------------------------------------------------------------
siblings([expr])取得一个包含匹配的元素集合中每一个元素的所有唯一同辈元素的元素集合。可以用可选的表达式进行筛选。
Get a set of elements containing all of the unique siblings of each of the matched set of elements. Can be filtered with an optional expressions.
返回值jQuery
参数expr (String) : (可选) 用于筛选同辈元素的表达式
示例找到每个div的所有同辈元素。
HTML 代码:
<p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>
jQuery 代码:
$("div").siblings()
结果:
[ <p>Hello</p>, <p>And Again</p> ]
找到每个div的所有同辈元素中带有类名为selected的元素。
HTML 代码:
<div><span>Hello</span></div><p>Hello Again</p><p>And Again</p>
jQuery 代码:
$("p").siblings(".selected")
结果:
[ <p>Hello Again</p> ]
------------------------------------------------------------------------------------------------------------------------
andSelf()
加入先前所选的加入当前元素中
对于筛选或查找后的元素,要加入先前所选元素时将会很有用。
Add the previous selection to the current selection.