活动链接 jquery superfish

发布于 2024-10-07 18:46:44 字数 1779 浏览 5 评论 0原文

我有一个问题。 我使用 jquery superfish 菜单,其结构如下,

<div id="menu" class="ge-navigation-item">
  <!-- navigation -->
    <ul id="test" class="sf-menu sf-vertical sf-js-enabled sf-shadow">
      <li><a href="index.html">Home</a></li>
      <li><a href="#">About</a>
        <ul>
          <li><a href="index.html">Index</a></li>
          <li><a href="#">Page</a>
                            <ul>
                                <li><a href="#">menu item</a></li>
                                <li><a href="#">menu item</a></li>
                                <li><a href="proef.html">menu item</a></li>
                                <li><a href="index.html">index</a></li>
                                <li><a href="#">menu item</a></li>
                            </ul>
          </li>
          <li><a href="page-fullwidth.php">Page - Full Width</a></li>
        </ul>
      </li>
      <li><a href="showcase.php">Showcase</a></li>
    </ul>
    <!-- /navigation -->
  </div>

我想要的是单击链接,菜单结构将可见

所以单击 index.html 将在所有带有 href index.html 的链接上设置一个链接 但是单击 proef.html 将在 href proef.html - 页面 - 关于上设置一个活动类

到目前为止,我已经尝试过此操作,但结果不佳

var path = window.location.toString().split("/")
path = path[path.length - 1]
//alert (path);
if (path)
$("#test a[href='" + path + "']").addClass("actief");
$("#test ul li:has(a) a[href='" + path +
     "']").parent().parent().parent().addClass("actief");

I have a question.
I use jquery superfish menu with the following structure

<div id="menu" class="ge-navigation-item">
  <!-- navigation -->
    <ul id="test" class="sf-menu sf-vertical sf-js-enabled sf-shadow">
      <li><a href="index.html">Home</a></li>
      <li><a href="#">About</a>
        <ul>
          <li><a href="index.html">Index</a></li>
          <li><a href="#">Page</a>
                            <ul>
                                <li><a href="#">menu item</a></li>
                                <li><a href="#">menu item</a></li>
                                <li><a href="proef.html">menu item</a></li>
                                <li><a href="index.html">index</a></li>
                                <li><a href="#">menu item</a></li>
                            </ul>
          </li>
          <li><a href="page-fullwidth.php">Page - Full Width</a></li>
        </ul>
      </li>
      <li><a href="showcase.php">Showcase</a></li>
    </ul>
    <!-- /navigation -->
  </div>

what I want is that clicking on a link the menu structure will be visible

So clicking on index.html will set a link on all links with href index.html
But clicking on proef.html will set an active class on href proef.html - Page - About

So far I have tried this but with bad results

var path = window.location.toString().split("/")
path = path[path.length - 1]
//alert (path);
if (path)
$("#test a[href='" + path + "']").addClass("actief");
$("#test ul li:has(a) a[href='" + path +
     "']").parent().parent().parent().addClass("actief");

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

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

发布评论

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

评论(2

寄风 2024-10-14 18:46:44

欢迎来到 stackoverflow。试试这个:

var path = window.location.pathname.split('/');
path = path[path.length-1];

if (path !== undefined) {
    $("#menu3").find("a[href='" + path + "']").addClass("actief");

}

这应该将该类应用于与当前 url 的最后一项匹配的任何链接。如果您正在寻找不同的行为,请详细说明!

更新
实际上,看起来您需要这样:

var path = window.location.pathname.split('/');
path = path[path.length-1];

if (path !== undefined) {
    $("#menu3")
        .find("a[href$='" + path + "']") // gets all links that match the href
        .parents('li')  // gets all list items that are ancestors of the link
        .children('a')  // walks down one level from all selected li's
        .addClass('actief');
}

这会将类放在链上的每个 a 元素上。

Welcome to stackoverflow. Try this:

var path = window.location.pathname.split('/');
path = path[path.length-1];

if (path !== undefined) {
    $("#menu3").find("a[href='" + path + "']").addClass("actief");

}

That should appply the class to any link that matches the last item of the current url. If you were looking for a different behavior, please elaborate!

UPDATE
Actually, it looks like you need this:

var path = window.location.pathname.split('/');
path = path[path.length-1];

if (path !== undefined) {
    $("#menu3")
        .find("a[href$='" + path + "']") // gets all links that match the href
        .parents('li')  // gets all list items that are ancestors of the link
        .children('a')  // walks down one level from all selected li's
        .addClass('actief');
}

That will put the class on every a element up the chain.

顾忌 2024-10-14 18:46:44

我们做了类似的事情:

$("#sample-menu-1").find("a[href='"+window.location.pathname+"']").each(function() {
$(this).parents('li').addClass("current");
$(this).closest('ul').css("visibility","visible").css("display","block");
});

它似乎有效,但将鼠标悬停在菜单上时,菜单不保持当前位置。

=== 更新 ===

鼠标悬停在菜单上后,父 ul 似乎被重置为不显示

we did something like:

$("#sample-menu-1").find("a[href='"+window.location.pathname+"']").each(function() {
$(this).parents('li').addClass("current");
$(this).closest('ul').css("visibility","visible").css("display","block");
});

It seems to work but on mousing over the menu, the menu doesn't hold the current position.

=== update ===

It seems the parent ul is reset to not display after mousing over the menu

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文