根据不与localhost一起使用的URL将活动类添加到当前页面链接

时间:2018-06-06 00:58:49

标签: jquery html hyperlink navigation anchor

我知道这是一个简单的jQuery任务,我已经完成了一百万次,但我似乎无法添加一个"活跃的"根据页面URL对下面的菜单项进行分类。这是使用http://localhost:8888/的问题吗?有2个索引页面,1个php 1 html是一个问题吗?我如何定位subnav?

  <nav class="header-menu menu">
                <ul>
                    <li class="menu-item-has-children">
                        <a href="/expertise">
                            <span class="nav-text">Expertise</span>
                            <span class="icon-down-arrow-small "></span>
                        </a>

                        <ul class="sub-menu">
                            <li><a href="">Meet our Team</a></li>
                            <li><a href="">Contact Us</a></li>
                            <li><a href="">Our Process</a></li>
                        </ul>

                    </li>
                    <li class="menu-item-has-children">
                        <a href="/projects">
                            <span class="nav-text">Projects</span>
                            <span class="icon-down-arrow-small "></span>
                        </a>
                        <ul class="sub-menu">
                            <li><a href="">Meet our Team</a></li>
                            <li><a href="">Contact Us</a></li>
                            <li><a href="">Our Process</a></li>
                            <li><a href="">Our Process</a></li>
                            <li><a href="">Our Process</a></li>
                        </ul>
                    </li>
                    <li>
                        <a href="/about">
                            <span class="nav-text">About</span>
                            <span class="icon-down-arrow-small "></span>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="nav-text">Insights</span>
                            <span class="icon-down-arrow-small "></span>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <span class="nav-text">Careers</span>
                            <span class="icon-down-arrow-small "></span>
                        </a>
                    </li>
                </ul>
            </nav>


      $(function() {
     var url = location.origin + location.pathname;
     $('nav a[href^="' + url + '"]')
      .closest('li')
       .addClass('active');
    });

0 个答案:

没有答案