开发者

how to find the anchor tag within the anchor tag?

开发者 https://www.devze.com 2023-01-31 04:45 出处:网络
<ul> <li class=\"topnav\">Fir开发者_JS百科stSet <ul> <li><a href=\'1.aspx\'/></li>
<ul>
 <li class="topnav">Fir开发者_JS百科stSet
   <ul>
     <li><a href='1.aspx'/></li>
     <li><a href='2.aspx'/></li>
   </ul>
 </li>
 <li class="topnav">SecondSet
   <ul>
     <li><a href='3.aspx'/></li>
     <li><a href='4.aspx'/></li>
   </ul>
 </li>
</ul>

this is my menu control, when i am on the 1.aspx/2.aspx i need to hightlight the menu FirstSet

$("#nav").find("a[href='" + window.location.pathname + "']").each(function() {
         $(this).addClass("activeTab");
});

using the abouve jquery code, i tried to add the class dynamically. Please let me know if i am missing somethignn in the above jquery.


You don't need the each() call:

$("#nav").find("a[href='" + window.location.pathname + "']").addClass("activeTab");

Depending on where nav is, you could probably do this:

$("#nav a[href='" + window.location.pathname + "']").addClass("activeTab");


If you're trying to add the class to the enclosing topnav.li, something like this should work:

$('#nav')
  .find('li.topnav')
  .has('a[href='+window.location.pathname+']')
  .addClass('activeTab');

This is plain string matching: be aware that something like a leading slash / in the window.location.pathname but not in your <a> tags will cause the match to fail.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号