Basically I’ve got two anchor tags separated by a div.
<a class="foo"></a>
<div class="count">0</a>
<a class="bar"></a>
and I’m doing something like this:
var addClass = $(this).parent().children('a:first-child').addClass('new');
It will then add it to the first anchor tag like so:
<a class="foo new"></a>
I know using the nth:child selector is pretty expensive, so I was wondering if there was an easier way to select that previous anchor node, (while ignoring the second one).
I’d probably combine
prevAllwithfirst(oreq(0), sincefirstjust callseq(0)— but I findfirstmore readable):prevAllreturns all of the element’s preceding siblings, in order starting with the closest sibling, and of coursefirst/eq(0)reduces the set to the first element.You might be tempted to use the
:firstselector withprevAllto avoid building up an unnecessary list of siblings, but to my surprise it works better to usefirstafter-the-fact.