Blah .... ...">

Selecting first anchor inside li element using jQuery

My HTML looks like this:

<li class="li-top"><a class="top sub" href=#>Blah</a> .... </li> 

I am trying to select an anchor tag to change the color of the text (“Blah”). But here's the catch: I use closest () because I start with a descendant of this li tag:

  $(this).closest('li.li-top'); 

How to get this anchor tag from this starting point? I tried next (), each (), children (), etc. I can’t get it. Thank you

+7
source share
2 answers

If you start with one of the children, you can try:

 $(this).parents('li.li-top').find('a:first'); 

I often happen to find "cousins ​​after removal" in the DOM.

+20
source

This method is probably:

 $('li.li-top a:first') 

Or:

 $(this).find('li.li-top a:first') 
+6
source

All Articles