gpt4 book ai didi

javascript - 比较 URL 和链接

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:07 25 4
gpt4 key购买 nike

我有以下 HTML:

<ul class="vertical_menu no_select">
<li class="edge_top">&nbsp;</li>
<!-- Menu Items Here -->
<li class="not_selected"><a class="selection_link" href="index.htm">Home</a></li>
<li class="not_selected"><a class="selection_link" href="index.htm">Subitem 1</a></li>
<li class="not_selected"><a class="selection_link" href="index.htm">Subitem 2</a></li>
<!-- Menu Items End Here -->
<li class="edge_bottom">&nbsp;</li>
</ul>

现在,类 "selection_link" 的链接需要有 li 元素(它们的父元素)才能有一个新类 "selected" 如果当前 url 匹配 anchor 标记内的 href 值。我可以获得完整的 URL,但我不知道如何使用它来帮助查看,因为在不同的目录级别可能有多个具有相同名称的页面。

如果您能提供帮助,谢谢!我将 jQuery 用于所有这些,所以请随时向我提供 jQuery 示例!

最佳答案

Check out the jsFiddle. This should do the trick:

$("ul.vertical_menu li a").each(function() {

if (this.href == window.location.href) {
$(this).parent().toggleClass("not_selected selected");
}

});

关于javascript - 比较 URL 和链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6125547/

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