jQuery API | 返回首页目录 | jQuery API 中英文对照版 |
prev(expr) |
prev(expr)
取得一个包含匹配的元素集合中每一个元素紧邻的前一个同辈元素的元素集合。 可以用一个可选的表达式进行筛选。 这个函数只返回前一个紧邻的同辈元素,而不是前面所有的同辈元素。 返回值:jQuery 参数:
找到每个段落紧邻的前一个同辈元素。 $("p").prev() HTML 代码: <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p> 结果: [ <div><span>Hello Again</span></div> ] 示例: 找到每个段落紧邻的前一个同辈元素中类名为selected的元素。 $("p").prev(".selected") HTML 代码: <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p> 结果: [ <p class="selected">Hello Again</p> ] 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. Return value: jQuery
Example: Find the very previous sibling of each paragraph. $("p").prev()Before: <p>Hello</p><div><span>Hello Again</span></div><p>And Again</p>Result: [ <div><span>Hello Again</span></div> ] Example: Find the very previous sibling of each paragraph that has a class "selected". $("p").prev(".selected") Before: <div><span>Hello</span></div><p class="selected">Hello Again</p><p>And Again</p>Result: [ <div><span>Hello</span></div> ] |