JQuery Tips(3)----关于$()包装集内元素的改变

<h1 class="postTitle" >JQuery Tips(3)----关于$()包装集内元素的改变</h1> <div class="clear" ></div> <div class="postBody" > <div id="cnblogs_post_body"> <h4>JQuery包装集内的元素在一开始的选定后,还可以通过一系列JQuery提供的方法对包装集内的元素进行扩充,修改,筛选,删除</h4> <h4><strong>find()方法 VS filter()方法</strong></h4> <p>这两个方法是比较容易搞混的.</p> <p>filter方法表示的是对当前内部的元素进行筛选,这个接受两种参数,一个返回bool的function,或者是JQuery的选择表达式,包装集内的元素只会小于等于当前包装集内的元素,并且含有的元素属于原来包装集内元素的子集:</p> <div > <pre> </pre> <div color:="" div="" id="&lt;span "> <div color:="" id="&lt;span ">the two</div> <div color:="" id="&lt;span ">the three</div> <script type="<span >text/javascript</span>"> alert($("<span >div</span>").filter("<span >:not(:first):not(:last)</span>").html()); <span >//out put<p>the two</p></span> alert($("<span >div</span>").filter(function() { <span >return</span> <span >this</span>.id == "<span >two</span>"; }).html());<span >//output <p>the two</p> as well</span> </script></div> <p>而find方法却是在当前元素内(子元素)部进行查找,并返回<strong>新的包装集</strong>,这意味着包装集可能会增加:</p> <div > <pre> </pre> <div color:="" div="" id="&lt;span "> <div color:="" id="&lt;span ">the two <p></p> <p></p> </div> <div color:="" id="&lt;span ">the three</div> <script type="<span >text/javascript</span>"> <span >alert</span>($("<span >div</span>").<span >find</span>("<span >p</span>").text()); <span >//alert "the twothe three"</span> <span >alert</span>($("<span >div</span>").<span >find</span>("<span >p</span>").<span >length</span>); <span >//alert 4 instead of original 3</span> </script></div> <p>从上面可以看出新包装集内的元素增加了</p> <p></p> <h4><strong>parents()方法 VS closest()方法</strong></h4> <p>这两个方法都是由当前元素向上查找所匹配的元素,不同之处如下:</p> <div > <pre> </pre> <div color:="" div="" id="&lt;span " the=""></div> </div> <script type="<span >text/javascript</span>"> <span >alert</span>($("<span >#p1</span>").parents("<span >div</span>").<span >length</span>); <span >//alert 2 include <div id="two"> and <div id="wrapper"></span> <span >alert</span>($("<span >#p1</span>").closest("<span >div</span>").<span >length</span>); <span >//alert 1 and only include <div id="two"></span> <span >alert</span>($("<span >#p1</span>").parents("<span >p</span>").<span >length</span>); <span >//alert 0 because it does not include current element</span> <span >alert</span>($("<span >#p1</span>").closest("<span >p</span>").<span >length</span>); <span >//alert 1 because it contain itself <p id="p1"></span> </script></div> <p>对于parents方法来说,会将当前元素向上的所有匹配元素加入新的包装集并返回,而closest方法只会包含离当前元素最近的元素,所以使用closest方法后当前包装集内的元素只能为1个或者0个</p> <p>而parents方法并不包括当前包装集内的元素,而closest方法会包含当前包装集内的元素</p> <h4><strong>直系子元素 VS 所有子元素</strong></h4> <p>使用children可以返回直系子元素,而用find加通配符的方法可以返回除了文本节点之外的所有子元素:</p> <div > <pre> </pre> <div color:="" div="" here="" id="&lt;span " p="" text="" the=""></div> </div> <script type="<span >text/javascript</span>"> <span >alert</span>($("<span >#wrapper</span>").children().<span >length</span>);<span >//alert 1 because only direct children included</span> <span >alert</span>($("<span >#wrapper</span>").<span >find</span>("<span >*</span>").<span >length</span>); <span >//alert 2 because all desendants included</span> <span >alert</span>($("<span >#wrapper</span>").<span >find</span>("<span >>*</span>").<span >length</span>);<span >//alert 1 because only direct children included</span> </script></div> <p>可以看出children方法只会含有当前元素的直系子元素,而使用find(&ldquo;&gt;*也会产生同样的效果&rdquo;).若想采纳所有的直系子元素直接在find内传&rdquo;*&rdquo;通配符</p> <h4><strong>回到过去的end()方法以及andself()方法</strong></h4> <p>上述所有的方法,以及比如add(),next(),nextAll(),prev()等对包装集内元素进行改变的方法都可以使用end()方法来进行返回:</p> <div > <pre> </pre> <div color:="" div="" here="" id="&lt;span " p="" text="" the=""></div> </div> <script type="<span >text/javascript</span>"> <span >alert</span>($("<span >#wrapper</span>").<span >find</span>("<span >>*</span>").end().get(0).id);<span >//alert "wrapper" instead of "two" because of end() method has been used</span> </script></div> <p>end()方法总是和最近的一个和包装集改变的方法相抵消,而抵消其他方法:</p> <div > <pre> </pre> <div color:="" div="" here="" id="&lt;span " p="" text="" the=""></div> </div> <script type="<span >text/javascript</span>"> <span >alert</span>($("<span >#wrapper</span>").<span >find</span>("<span >#p1</span>").html("<span >new value</span>").end().get(0).id);<span >//alert wrapper because end method</span> <span >alert</span>($("<span >#p1</span>").text())<span >//alert new value bacause the html method in previous has not been cancelled</span> </script></div> <p>如果需要在改变包装集内元素的情况下还需要包含原始的包装集内元素,使用andself方法:</p> <div > <pre> </pre> <div color:="" div="" here="" id="&lt;span " p="" text="" the=""></div> </div> <script type="<span >text/javascript</span>"> <span >var</span> $a = $("<span >#wrapper</span>").<span >find</span>("<span >#two</span>").andSelf(); <span >alert</span>($a[0].id);<span >//alert two first</span> <span >alert</span>($a[1].id);<span >//alert wrapper after that</span> </script> <p>我们会发现首先alert two,因为two先被选择</p>
RangeTime:0.005916s
RangeMem:211.57 KB
返回顶部 留言