gpt4 book ai didi

javascript - 将事件类添加到 href

转载 作者:行者123 更新时间:2023-11-30 19:07:51 25 4
gpt4 key购买 nike

这段代码有一个小问题,

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>

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

最佳答案

你快到了。你需要find addClass 之前的 anchor 标记。

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

关于javascript - 将事件类添加到 href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58797629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com