根据上一页href将活动类添加到元素

时间:2018-02-06 13:41:10

标签: javascript jquery css twitter-bootstrap

在菜单项中,我有一个如下所示的下拉菜单。 点击" General Item"系统进入页面" profile"有一些标签,这工作正常。

但是当"项目1"点击我想去那个"个人资料"页面,但显示对应于"项目1和#34;积极的。同样,当点击"项目2"时,显示对应于"项目2"积极的。 你知道怎么做吗?

下拉菜单:

<li class="nav-item dropdown">
    <a class="nav-link dropdown-toggle" href="#" id="userDropdown"  data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        User
    </a>
    <div class="dropdown-menu" aria-labelledby="userDropdown">
        <a class="dropdown-item text-gray" href="/profile/item1">Item 1</a>
        <a class="dropdown-item text-gray" href="/profile/item2">Item 2</a>
        <a class="dropdown-item text-gray" href="/profile">General Item</a>
    </div>
</li>

带有一些标签的页面是这样的:

<div class="container nopadding py-4">
  <div class="row mt-3">
    <div class="col-12">
      <ul class="nav nav-pills bg-light-gray registration_form_list" role="tablist">
        <li class="">
          <a class="nav-link active" href="#item1" data-toggle="tab" role="tab">
             <span class="d-none d-lg-inline-block">Item 1</span></a>
        </li>
        <li class="disabled">
          <a class="nav-link" href="#item2" data-toggle="tab" role="tab">
             <span class="d-none d-lg-inline-block">Item 2</span></a>
        </li>

        <li class="disabled">
          <a class="nav-link" href="#item3" data-toggle="tab" role="tab">
             <span class="d-none d-lg-inline-block">Item 3</span></a>
        </li>
      </ul>
      <div class="tab-content registration_body bg-white" id="myTabContent">
        <div class="tab-pane fade show active clearfix" id="item1" role="tabpanel"
             aria-labelledby="home-tab">

          Content 1
        </div>
        <div class="tab-pane fade clearfix" id="item2" role="tabpanel"
             aria-labelledby="profile-tab">
          Content 2

        </div>
        <div class="tab-pane clearfix fade" id="item3" role="tabpanel"
             aria-labelledby="contact-tab">
          Content 3
        </div>
      </div>
    </div>
  </div>
</div>

JS:

$(function($) {
     var path = window.location.href; 
     $('.registration_form_list a').each(function() {
      if (this.href === path) {
       $(this).addClass('active');
      }
     });
});

0 个答案:

没有答案