前端开发利器jQuery-筛选器之查找

2,009次阅读
没有评论

 

前端开发利器jQuery-筛选器之查找

jQuery筛选器之查找

筛选器 描述
$("SET").children([expr]); 取得一个包含匹配的元素集合中每一个元素的所有子元素的元素集合
$("SET").find(expr/obj/ele); 搜索所有与指定表达式匹配的元素
$("SET").closest(expr/object/element); 从元素本身开始,逐级向上级元素匹配,并返回最先匹配的元素
$("SET").next([expr]); 取得一个包含匹配的元素集合中每一个元素紧邻的后面同辈元素的元素集合
$("SET").nextAll([expr]); 查找当前元素之后所有的同辈元素
$("SET").nextUntil([exp/ele][,fil]); 查找当前元素之后所有的同辈元素,直到遇到匹配的那个元素为止
$("SET").offsetParent(); 返回第一个匹配元素用于定位的父节点
$("SET").parent([expr]); 取得一个包含着所有匹配元素的唯一父元素的元素集合
$("SET").parents([expr]); 取得一个包含着所有匹配元素的祖先元素的元素集合(不包含根元素)。
$("SET").parentsUntil([expr/element][,filter]); 查找当前元素的所有的父辈元素,直到遇到匹配的那个元素为止
$("SET").prev([expr]); 取得一个包含匹配的元素集合中每一个元素紧邻的前一个同辈元素的元素集合
$("SET").prevAll([expr]); 查找当前元素之前所有的同辈元素
$("SET").prevUntil([exp/ele][,fil]); 查找当前元素之前所有的同辈元素,直到遇到匹配的那个元素为止
$("SET").siblings([expr]); 取得一个包含匹配的元素集合中每一个元素的所有唯一同辈元素的元素集合

jQuery筛选器之查找示例

  • $("SET").children([expr]);

可以通过可选的表达式来过滤所匹配的子元素。注意:parents()将查找所有祖辈元素,而children()只考虑子元素而不考虑所有后代元素。

参数 描述
expr 用以过滤子元素的表达式

查找DIV中的每个子元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>Hello Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">p</span>></span>And Again<span class="tag"></<span class="name">p</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"div"</span>).children();</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">span</span>></span>Hello Again<span class="tag"></<span class="name">span</span>></span> ]</span>

在每个div中查找 .selected 的类。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>Hello<span class="tag"></<span class="name">span</span>></span><span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">p</span>></span>And Again<span class="tag"></<span class="name">p</span>></span><span class="tag"></<span class="name">div</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"div"</span>).children(<span class="string">".selected"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span> ]</span>
  • $("SET").find(expr/obj/ele);

这个函数是找出正在处理的元素的后代元素的好方法,所有搜索都依靠jQuery表达式来完成。这个表达式可以使用CSS1-3的选择器语法来写。

参数 描述
expr 用于查找的表达式
jQuery object 一个用于匹配元素的jQuery对象
element 一个DOM元素

从所有的段落开始,进一步搜索下面的span元素。与$("p span");相同。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">p</span>></span><span class="tag"><<span class="name">span</span>></span>Hello<span class="tag"></<span class="name">span</span>></span>, how are you?<span class="tag"></<span class="name">p</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"p"</span>).find(<span class="string">"span"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">span</span>></span>Hello<span class="tag"></<span class="name">span</span>></span> ]</span>
  • $("SET").closest(expr/object/element);

closest会首先检查当前元素是否匹配,如果匹配则直接返回元素本身。如果不匹配则向上查找父元素,一层一层往上,直到找到匹配选择器的元素。如果什么都没找到则返回一个空的jQuery对象。closest对于处理事件委托非常有用。

参数 描述
expr 用以过滤元素的表达式。
jQuery object 一个用于匹配元素的jQuery对象
element 一个用于匹配元素的DOM元素

展示如何使用clostest查找多个元素

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">ul</span>></span><span class="tag"><<span class="name">li</span>></span><span class="tag"></<span class="name">li</span>></span><span class="tag"><<span class="name">li</span>></span><span class="tag"></<span class="name">li</span>></span><span class="tag"></<span class="name">ul</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"li:first"</span>).closest([<span class="string">"ul"</span>, <span class="string">"body"</span>]);</span>

结果:

<span class="line">1</span>
<span class="line">[ul, body]</span>

展示如何使用clostest来完成事件委托。

HTML代码:

<span class="line">1</span>
2
3
4
<span class="line"><span class="tag"><<span class="name">ul</span>></span></span>
<span class="line">    <span class="tag"><<span class="name">li</span>></span><span class="tag"><<span class="name">b</span>></span>Click me!<span class="tag"></<span class="name">b</span>></span><span class="tag"></<span class="name">li</span>></span></span>
<span class="line">    <span class="tag"><<span class="name">li</span>></span>You can also <span class="tag"><<span class="name">b</span>></span>Click me!<span class="tag"></<span class="name">b</span>></span><span class="tag"></<span class="name">li</span>></span></span>
<span class="line"><span class="tag"></<span class="name">ul</span>></span></span>

jQuery代码:

<span class="line">1</span>
2
3
<span class="line">$(<span class="built_in">document</span>).bind(<span class="string">"click"</span>, <span class="function"><span class="keyword">function</span> (<span class="params">e</span>) </span>{</span>
<span class="line">    $(e.target).closest(<span class="string">"li"</span>).toggleClass(<span class="string">"hilight"</span>);</span>
<span class="line">});</span>
  • $("SET").next([expr]);

这个函数只返回后面那个紧邻的同辈元素,而不是后面所有的同辈元素(可以使用nextAll)。可以用一个可选的表达式进行筛选。

参数 描述
expr 用于筛选的表达式

找到每个段落的后面紧邻的同辈元素

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">p</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>And Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"p"</span>).next();</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">p</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span>, <span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>And Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span> ]</span>

找到每个段落的后面紧邻的同辈元素中类名为selected的元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>And Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"p"</span>).next(<span class="string">".selected"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span> ]</span>
  • $("SET").nextAll([expr]);
属性 描述
expr 用来过滤的表达式

给第一个div之后的所有元素加个类

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"div:first"</span>).nextAll().addClass(<span class="string">"after"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"after"</span>></span><span class="tag"></<span class="name">div</span>></span>, <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"after"</span>></span><span class="tag"></<span class="name">div</span>></span>, <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"after"</span>></span><span class="tag"></<span class="name">div</span>></span> ]</span>
  • $("SET").nextUntil([exp|ele][,fil]);

如果提供的jQuery代表了一组DOM元素,.nextUntil()方法也能让我们找遍所有元素所在的DOM树,直到遇到了一个跟提供的参数匹配的元素的时候才会停下来。这个新jQuery对象里包含了下面所有找到的同辈元素,但不包括那个选择器匹配到的元素。

如果没有选择器匹配到,或者没有提供参数,那么跟在后面的所有同辈元素都会被选中。这就跟用没有提供参数的.nextAll()效果一样。

[expr][,filter]

  • expr: 用于筛选祖先元素的表达式
  • filter: 一个字符串,其中包含一个选择表达式匹配元素|

[element][,filter]

  • element: 用于筛选祖先元素的DOM元素。
  • filter: 一个字符串,其中包含一个选择表达式匹配元素。

#term-2后面直到dt前的元素加上红色背景

HTML代码:

<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="line"><span class="tag"><<span class="name">dl</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dt</span>></span>term 1<span class="tag"></<span class="name">dt</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-a<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-b<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-c<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-d<span class="tag"></<span class="name">dd</span>></span></span>

<span class="line">  <span class="tag"><<span class="name">dt</span> <span class="attr">id</span>=<span class="string">"term-2"</span>></span>term 2<span class="tag"></<span class="name">dt</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 2-a<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 2-b<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 2-c<span class="tag"></<span class="name">dd</span>></span></span>

<span class="line">  <span class="tag"><<span class="name">dt</span>></span>term 3<span class="tag"></<span class="name">dt</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 3-a<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 3-b<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line"><span class="tag"></<span class="name">dl</span>></span></span>

jQuery代码:

<span class="line">1</span>
2
3
4
<span class="line">$(<span class="string">'#term-2'</span>).nextUntil(<span class="string">'dt'</span>).css(<span class="string">'background-color'</span>, <span class="string">'red'</span>);</span>

<span class="line"><span class="keyword">var</span> term3 = <span class="built_in">document</span>.getElementById(<span class="string">"term-3"</span>);</span>
<span class="line">$(<span class="string">"#term-1"</span>).nextUntil(term3, <span class="string">"dd"</span>).css(<span class="string">"color"</span>, <span class="string">"green"</span>);</span>
  • $("SET").offsetParent();

这返回父元素中第一个其position设为relative或者absolute的元素。此方法仅对可见元素有效。

设置最近的祖先定位元素的背景颜色

HTML代码:

<span class="line">1</span>
2
3
4
5
6
<span class="line"><span class="tag"><<span class="name">div</span> <span class="attr">style</span>=<span class="string">"width:70%;position:absolute;left:100px;top:100px"</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">div</span> <span class="attr">style</span>=<span class="string">"margin:50px;background-color:yellow"</span>></span></span>
<span class="line">     <span class="tag"><<span class="name">p</span>></span>点击下面的按钮可以设置本段落的最近的父(祖先)元素的背景色。<span class="tag"></<span class="name">p</span>></span></span>
<span class="line">   <span class="tag"><<span class="name">div</span>></span></span>
<span class="line"><span class="tag"></<span class="name">div</span>></span></span>
<span class="line"><span class="tag"><<span class="name">button</span>></span>点击这里<span class="tag"></<span class="name">button</span>></span></span>

jQuery代码:

<span class="line">1</span>
2
3
<span class="line">$(<span class="string">"button"</span>).click(<span class="function"><span class="keyword">function</span>()</span>{</span>
<span class="line">    $(<span class="string">"p"</span>).offsetParent().css(<span class="string">"background-color"</span>,<span class="string">"red"</span>);</span>
<span class="line">});</span>
  • $("SET").parent([expr]);
参数 描述
expr 用来筛选的表达式

查找每个段落的父元素

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"></<span class="name">div</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"p"</span>).parent();</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"></<span class="name">div</span>></span>]</span>

查找段落的父元素中每个类名为selected的父元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span><span class="tag"><<span class="name">p</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span><span class="tag"></<span class="name">div</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"p"</span>).parent(<span class="string">".selected"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span><span class="tag"><<span class="name">p</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span><span class="tag"></<span class="name">div</span>></span> ]</span>
  • $("SET").parents([expr]);
参数 描述
expr 用于筛选祖先元素的表达式

找到每个span元素的所有祖先元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">html</span>></span><span class="tag"><<span class="name">body</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">p</span>></span><span class="tag"><<span class="name">span</span>></span>Hello<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">span</span>></span>Hello Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"></<span class="name">body</span>></span><span class="tag"></<span class="name">html</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"span"</span>).parents();</span>

找到每个span的所有是p元素的祖先元素。

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"span"</span>).parents(<span class="string">"p"</span>);</span>
  • $("SET").parentsUntil([expr/element][,filter]);

如果提供的jQuery代表了一组DOM元素,.parentsUntil()方法也能让我们找遍所有元素的祖先元素,直到遇到了一个跟提供的参数匹配的元素的时候才会停下来。这个返回的jQuery对象里包含了下面所有找到的父辈元素,但不包括那个选择器匹配到的元素。

[expr][,filter]

  • expr: 用于筛选祖先元素的表达式
  • filter: 一个字符串,其中包含一个选择表达式匹配元素。

[element][,filter]

  • element: 用于筛选祖先元素的DOM元素
  • filter: 一个字符串,其中包含一个选择表达式匹配元素。

查找item-a的祖先,但不包括level-1

HTML代码:

<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<span class="line"><span class="tag"><<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"level-1"</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-i"</span>></span>I<span class="tag"></<span class="name">li</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-ii"</span>></span>II</span>
<span class="line">    <span class="tag"><<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"level-2"</span>></span></span>
<span class="line">      <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-a"</span>></span>A<span class="tag"></<span class="name">li</span>></span></span>
<span class="line">      <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-b"</span>></span>B</span>
<span class="line">        <span class="tag"><<span class="name">ul</span> <span class="attr">class</span>=<span class="string">"level-3"</span>></span></span>
<span class="line">          <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-1"</span>></span>1<span class="tag"></<span class="name">li</span>></span></span>
<span class="line">          <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-2"</span>></span>2<span class="tag"></<span class="name">li</span>></span></span>
<span class="line">          <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-3"</span>></span>3<span class="tag"></<span class="name">li</span>></span></span>
<span class="line">        <span class="tag"></<span class="name">ul</span>></span></span>
<span class="line">      <span class="tag"></<span class="name">li</span>></span></span>
<span class="line">      <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-c"</span>></span>C<span class="tag"></<span class="name">li</span>></span></span>
<span class="line">    <span class="tag"></<span class="name">ul</span>></span></span>
<span class="line">  <span class="tag"></<span class="name">li</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">li</span> <span class="attr">class</span>=<span class="string">"item-iii"</span>></span>III<span class="tag"></<span class="name">li</span>></span></span>
<span class="line"><span class="tag"></<span class="name">ul</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">'li.item-a'</span>).parentsUntil(<span class="string">'.level-1'</span>).css(<span class="string">'background-color'</span>, <span class="string">'red'</span>);</span>
  • $("SET").prev([expr]);

可以用一个可选的表达式进行筛选。只有紧邻的同辈元素会被匹配到,而不是前面所有的同辈元素

属性 描述
expr 用于筛选前一个同辈元素的表达式

找到每个段落紧邻的前一个同辈元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>Hello Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">p</span>></span>And Again<span class="tag"></<span class="name">p</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"p"</span>).prev();</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>Hello Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span> ]</span>

找到每个段落紧邻的前一个同辈元素中类名为selected的元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>Hello<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">p</span>></span>And Again<span class="tag"></<span class="name">p</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"p"</span>).prev(<span class="string">".selected"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span> ]</span>
  • $("SET").prevAll([expr]);
属性 描述
expr 用于过滤的表达式

给最后一个之前的所有div加上一个类

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"></<span class="name">div</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"div:last"</span>).prevAll().addClass(<span class="string">"before"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"before"</span>></span><span class="tag"></<span class="name">div</span>></span>, <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"before"</span>></span><span class="tag"></<span class="name">div</span>></span>, <span class="tag"><<span class="name">div</span> <span class="attr">class</span>=<span class="string">"before"</span>></span><span class="tag"></<span class="name">div</span>></span> ]</span>
  • $("SET").prevUntil([exp/ele][,fil]);

如果提供的jQuery代表了一组DOM元素,.prevUntil()方法也能让我们找遍所有元素所在的DOM树,直到遇到了一个跟提供的参数匹配的元素的时候才会停下来。这个新jQuery对象里包含了前面所有找到的同辈元素,但不包括那个选择器匹配到的元素。

如果没有选择器匹配到,或者没有提供参数,那么排在前面的所有同辈元素都会被选中。这就跟用没有提供参数的.prevAll()效果一样。

[expr][,filter]

  • expr: 用于筛选祖先元素的表达式
  • filter: 一个字符串,其中包含一个选择表达式匹配元素。

[element][,filter]

  • element: 用于筛选祖先元素的DOM元素
  • filter: 一个字符串,其中包含一个选择表达式匹配元素。

#term-2前面直到dt前的元素加上红色背景

HTML代码:

<span class="line">1</span>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<span class="line"><span class="tag"><<span class="name">dl</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dt</span>></span>term 1<span class="tag"></<span class="name">dt</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-a<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-b<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-c<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 1-d<span class="tag"></<span class="name">dd</span>></span></span>

<span class="line">  <span class="tag"><<span class="name">dt</span> <span class="attr">id</span>=<span class="string">"term-2"</span>></span>term 2<span class="tag"></<span class="name">dt</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 2-a<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 2-b<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 2-c<span class="tag"></<span class="name">dd</span>></span></span>

<span class="line">  <span class="tag"><<span class="name">dt</span>></span>term 3<span class="tag"></<span class="name">dt</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 3-a<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line">  <span class="tag"><<span class="name">dd</span>></span>definition 3-b<span class="tag"></<span class="name">dd</span>></span></span>
<span class="line"><span class="tag"></<span class="name">dl</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">'#term-2'</span>).prevUntil(<span class="string">'dt'</span>).css(<span class="string">'background-color'</span>, <span class="string">'red'</span>);</span>
  • $("SET").siblings([expr]);

找到每个div的所有同辈元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>Hello Again<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">p</span>></span>And Again<span class="tag"></<span class="name">p</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"div"</span>).siblings();</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">p</span>></span>Hello<span class="tag"></<span class="name">p</span>></span>, <span class="tag"><<span class="name">p</span>></span>And Again<span class="tag"></<span class="name">p</span>></span> ]</span>

找到每个div的所有同辈元素中带有类名为selected的元素。

HTML代码:

<span class="line">1</span>
<span class="line"><span class="tag"><<span class="name">div</span>></span><span class="tag"><<span class="name">span</span>></span>Hello<span class="tag"></<span class="name">span</span>></span><span class="tag"></<span class="name">div</span>></span><span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span><span class="tag"><<span class="name">p</span>></span>And Again<span class="tag"></<span class="name">p</span>></span></span>

jQuery代码:

<span class="line">1</span>
<span class="line">$(<span class="string">"div"</span>).siblings(<span class="string">".selected"</span>);</span>

结果:

<span class="line">1</span>
<span class="line">[ <span class="tag"><<span class="name">p</span> <span class="attr">class</span>=<span class="string">"selected"</span>></span>Hello Again<span class="tag"></<span class="name">p</span>></span> ]</span>
admin
版权声明:本站原创文章,由admin2016-07-16发表,共计7100字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)