jquery find()和filter()的區別

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

在寫js尋找當前標籤下某一子項目的子項目集合時,用到了此方法

1 $(".flyout-trigger").mouseover(function(){2    var maxnum=$(this).find(".flyout-link").children().filter("dt").size();3    $(this).children(".flyout-link").show();4    $(this).children(".flyout-link").animate({5        height:maxnum*26+‘px‘6    });7  });

 

搜了一下關於find()和filter()的用法

函數的區別,文檔上面是這麼寫的:

find(expr):搜尋所有與指定運算式匹配的元素。這個函數是找出正在處理的元素的後代元素的好方法

filter(expr):篩選出與指定運算式匹配的元素集合。這個方法用於縮小匹配的範圍。用逗號分隔多個運算式

 1 <html> 2 <head> 3 <script src="http://code.jquery.com/jquery-latest.js"></script> 4 <script type="text/javascript"> 5     $(function(){ 6        alert($("p").find(".selected").html()); // find html 7        alert($("p").filter(".selected").html()); // filter html 8     }); 9 </script>10 </head>11 <body>12 <p>html</p>13 <p><span class="selected">find html</span></p>14 <p class="selected">filter html</p>15 </body>16 </html>

也許你已經看出它們的區別了,find()會在p元素內尋找class為selected的元素。

而filter()則是篩選p的class為selected的元素。

一個是對它的子集操作,一個是對自身集合元素篩選.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.