jQuery filtering selector to remove nested elements matching pattern

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

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

6条回答
  •  有刺的猬
    2020-12-17 02:48

    If the .inner are always direct children of .outers - children() is probably your best bet (jasongetsdown's answer)

    If you need something that looks even deeper, you could do something like this:

    var $outer = $('.outer').first(); // grab the first .outer
    $outer.find('.inner').filter(function() {
        // only if the closest parent .outer is the same as the .outer we are looking in
        return $(this).closest('.outer').get(0) == $outer.get(0);
    }).css('border','1px solid #000');
    

    jsfiddle demo

提交回复
热议问题