jquery中find和filter有什么区别?

Bho*_*yar 0 jquery

以下代码中哪种方法最好:

$('#navs li').find('.activenav').removeClass('activenav');
$('#navs li').filter('.activenav').removeClass('activenav');
Run Code Online (Sandbox Code Playgroud)

或者我可以像这样使用

$('#navs li').find('.activenav').filter('.activenav').removeClass('activenav');

or 

$('#navs li').filter('.activenav').find('.activenav').removeClass('activenav');
Run Code Online (Sandbox Code Playgroud)

如果我这样做会怎么样?


更新

我可以绑定到find和filter,因为我的代码有时需要查找,有时需要进行过滤

she*_*nan 6

find方法搜索当前匹配元素的匹配元素.

filter方法搜索匹配元件的电流匹配的元素.

考虑这个HTML:

<div class="foo">
  <a class="foo">Bar</a>
</div>
Run Code Online (Sandbox Code Playgroud)

而这个jQuery:

$('div').filter('.foo').hide(); // hides the div with the class "foo"

$('div').find('.foo').hide(); // hides the child anchor with the class "foo"
Run Code Online (Sandbox Code Playgroud)

请注意,两者都会导致隐藏元素,但它们会针对不同的元素.