将活动类添加到 href

我对这段代码有一个小问题,


jQuery('.nav-menu  li:has(a[href="'+ window.location.pathname +'"])').addClass('active');

它给了我这样的结果:


<li class="active">

  <a href="/someUrl">Test</a>

 </li>

但我想要这样的东西:


 <li>

  <a href="/someUrl" class="active">Test</a>

 </li>

有人可以帮我解决这个问题吗?


慕姐8265434
浏览 217回答 2
2回答

元芳怎么了

你快到了。您需要在之前找到锚标记addClass。jQuery('.nav-menu&nbsp;li:has(a[href="'+&nbsp;window.location.pathname&nbsp;+'"])').find('a').addClass('active');

慕的地6264312

li在选择器中添加一个标签。jQuery('.nav-menu&nbsp;&nbsp;li:has(a[href="'+&nbsp;window.location.pathname&nbsp;+'"])&nbsp;a').addClass('active');
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript