You can use the :first selector to select only the first matching .block element:
$('.block:first')
This works because jQuery matches elements in the order of the document. The outermost .block element will be the first element matched by .block , and :first will only filter to return it.
Note that :first does not match :first-child .
EDIT . In response to your update, you can write the following, which will only work if all the elements are nested in three depths:
$('.block:note(:has(.block .block))')
You can write a more robust solution using a function call:
$('.block').not(function() { return $(this).closest('.block').length; })
This will find all .block elements, and then remove any matched elements that have a match with the .block ancestor. (You can replace closest with parent if you want).
SLaks
source share