jQuery从#find结果中排除选择器匹配

时间:2012-04-02 18:10:44

标签: jquery find

如果给出这样的结构,我最好如何从#other_section中排除结果?

<form>
  <input id=one>
  <div id=other_section>
    <input id=two>
  </div>
</form>

这是一个可能的API:

inputs = @find(":input").exclude("#other_section :input")

2 个答案:

答案 0 :(得分:2)

$(':input:not(#other_section > :input)');

示例:http://jsfiddle.net/5XCcF/

答案 1 :(得分:0)

这是我的解决方案:

$.fn.exclude = (selector)->
  excluded_items = @end().find(selector)
  @filter ->
    !_.include(excluded_items, this)