【问题标题】:Highlight link in sidebar based on page基于页面在侧边栏中突出显示链接
【发布时间】:2016-02-03 02:54:20
【问题描述】:

我正在构建一个侧边栏导航,用于跟踪您在网站上的位置。我有一个下拉菜单,当您将鼠标悬停在它上面时会展开。我希望导航栏中的链接突出显示以指示您在哪个页面上。我让它适用于父页面,但是如果您将鼠标悬停在下拉菜单上并选择一个子链接,则该链接不会突出显示并且不会自动展开菜单以显示您在该页面上。

我把我的代码放在 JSfiddle 中。 https://jsfiddle.net/6wrn5hgg/2/

这里是我正在使用的代码的快速浏览。

//Highlight current page
$(function() {
  $('a').each(function() {
    if ($(this).prop('href') == window.location.href) {
      $(this).addClass('current');
    }
  });
});

//subbar menu drop down
$(document).ready(function() {

  $('li.parent').hover(function() {
    $(this).siblings().find('.subnav').slideUp();
    $(this).children('.subnav').slideDown();

    if ($(this).children('.subnav').slideDown() == window.location.href) {
      $(this).addClass('current').slideDown();
    }
  });

});

如何使菜单显示红色背景并自动展开导航?

【问题讨论】:

  • 你所有的<a > 标签只有href #
  • 我只是这样做,因为我没有任何地方可以在 jsfiddle 中将其链接到。它需要实际链接才能工作吗?
  • window.location.href 会给你绝对路径。所以你要么需要把绝对路径作为href,要么你需要在window.location.href中找到href的索引。这是一个例子:jsfiddle.net/DinoMyte/6wrn5hgg/3

标签: javascript jquery navbar


【解决方案1】:

为了实现您的目标,以下是选项:

选项1:使用绝对网址:

检查window.location.href的值是否与anchor标签的href的值完全相同。

<li class="parent"><a href="https://fiddle.jshell.net/_display/" class="">About Us <span class="caret"></span></a>  // with absolute url 

    $(document).ready(function() {

      $('.parent a').each(function() {
        if ($(this).prop('href') == window.location.href) {
           $(this).addClass('current');
           $(this).closest('.parent').find('ul.subnav').slideDown();
        }
      });

      $('li.parent').hover(function() {
        $(this).siblings().find('.subnav').slideUp();
        $(this).children('.subnav').slideDown();

        if ($(this).children('.subnav').slideDown() == window.location.href) {
          $(this).addClass('current').slideDown();
        }
      });

    });

例如:https://jsfiddle.net/DinoMyte/6wrn5hgg/4/

选项 2:使用相对网址:

检查window.location.href的值是否包含anchor标签的href的值。

<li class="parent"><a href="/_display/" class="">About Us <span class="caret"></span></a>  // with relative url



$(document).ready(function() {

  $('.parent a').each(function() {
    if (window.location.href.indexOf($(this).prop('href')) != -1) {
       $(this).addClass('current');
       $(this).closest('.parent').find('ul.subnav').slideDown();
    }
  });

  $('li.parent').hover(function() {
    $(this).siblings().find('.subnav').slideUp();
    $(this).children('.subnav').slideDown();

    if ($(this).children('.subnav').slideDown() == window.location.href) {
      $(this).addClass('current').slideDown();
    }
  });

});

例如:https://jsfiddle.net/DinoMyte/6wrn5hgg/5/

【讨论】:

  • 太棒了!谢谢!我的路径是相对的,所以我使用选项 2。导航栏现在自动展开,但它没有突出显示链接。有什么想法吗?
  • 检查console.log($(this).prop('href')) 和console.log(window.location.href) 并检查第二个日志值是否包含第一个日志值。
  • 第一个 console.log 显示未定义。但第二个是给我整个网址
  • a标签中href的值是多少?
  • 你有 href 的绝对路径,而不是 relative 。 'dev.peakalarm.com/peakav'
【解决方案2】:

window.location.href&lt;a&gt; 标签的href 并不完全相同。请改用indexOf() 函数。

$('.parent > a').each(function() {
   if (window.location.href.indexOf(this.href) > -1) {
       $(this).addClass('current');
   }
});

【讨论】:

  • 将 html 也添加到您的答案中以了解 indexOf 的概念,以便 OP 理解。
  • 在这种情况下,您的解决方案将无法正常工作,因为他的 href = "#" 在 window.location.href 中的索引为 -1
  • 浏览问题的cmets。他说这只是为了小提琴。在他的原始代码中,他有实际的href。 @DinoMyte
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-13
  • 1970-01-01
相关资源
最近更新 更多