比较 URL 和链接

发布于 2024-11-09 19:12:47 字数 894 浏览 5 评论 0原文

我有以下 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 元素(其父元素)才能选择新的类 " " 如果当前 url 与锚标记内的 href 值匹配。我可以获得完整的 URL,但我不知道如何使用它来帮助查看,因为在不同的目录级别可能有多个具有相同名称的页面。

如果您能提供帮助,谢谢!我正在使用 jQuery 来完成这一切,所以请随时为我提供 jQuery 示例!

I have the following HTML:

<ul class="vertical_menu no_select">
    <li class="edge_top"> </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"> </li>
</ul>

Now, the links with the class "selection_link" need to have the li element (their parent) to have a new class "selected" if the current url matches the href value inside of the anchor tag. I can get the full URL but I don't know how to use that to help seeing as there could be more than one page with the same name at different directory levels.

Thanks if you can help! I'm using jQuery with all of this so feel free to provide me with jQuery examples!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

狼性发作 2024-11-16 19:12:47

查看 jsFiddle。这应该可以解决问题:

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

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

});

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");
    }

});
十二 2024-11-16 19:12:47
$('.selection_link').each(function() {
    if($(this).attr('href') === window.location.pathName.substring(1)) {
        $(this).parent().attr("class", "selected");
    }
});

未经测试,但它应该让你开始。 substring 是因为路径名以“/”开头,您也可以将其放入 href 中

$('.selection_link').each(function() {
    if($(this).attr('href') === window.location.pathName.substring(1)) {
        $(this).parent().attr("class", "selected");
    }
});

not tested, but it'll it should start you off. substring is because pathname starts with '/' you could just put that in your href's too

深爱成瘾 2024-11-16 19:12:47

或者这个...

$('.vertical_menu a').each(function() {
    if (location.href.search(this.attr('href')) != -1) {
        $(this).parent().addClass('selected');
    }
});

or this...

$('.vertical_menu a').each(function() {
    if (location.href.search(this.attr('href')) != -1) {
        $(this).parent().addClass('selected');
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文