jquery - how to select two levels of hierarchy of the html? -
suppose, html...
<div id="main"> <div><!--should select this--> <article>should not select this</article> </div> <div><!--should not select this--> <article>should select this</article> <article>should select this</article> </div> </div>
to re-declare question: want select article's 1 level parent if article 1 , if article more 1 it's 1 level parent should not selected instead article should selected.
i need cleaner way select selector.
try
var $els = $('#main').find('> div, > div > article').filter(function () { return this.tagname == 'div' ? $(this).children().length == 1 : $(this).siblings().length > 0 }); console.log($els.get())
demo: fiddle
Comments
Post a Comment