jQuery filtering selector to remove nested elements matching pattern

前端 未结 6 832
再見小時候
再見小時候 2020-12-17 01:58

Given this sample markup (assuming a random number of elements between .outer and .inner:

6条回答
  •  甜味超标
    2020-12-17 02:41

    Here's another option. Suppose you have the .outer o, this will select all inners under it:

    o.find('.inner').not(o.find('.outer .inner'))
    

    It should work identically to gnarf's answer, but a bit simpler.

    First, it finds all inners under this outer.
    Next, remove all inners that are descendants of other outers

    Interactive working example: http://jsfiddle.net/Zb9gF/

    Selector performance seems to be much better using this method as opposed to the .filter() as well: http://jsperf.com/selector-test-find-not

提交回复
热议问题