標籤: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的元素。
一個是對它的子集操作,一個是對自身集合元素篩選.