繁体   English   中英

根据当前网址激活标签

[英]Turn the tab active based on the current url

我在“#registration_form_list”列表中有一些列表项。 默认活动列表项是ID为“#generalInfo”的列表项。 但我希望当访问这样的URL“ http://proj.test/user/profile?user = 1 #myTickets ”时,保持活动状态的选项卡是“我的票证”选项卡。

所以我想根据当前的网址激活标签。 但它不适用于下面的jQuery。

你知道为什么吗?

HTML:

<ul class="nav nav-pills bg-light-gray registration_form_list" role="tablist">
    <li class="">
        <a class="nav-link active" href="#generalInfo" data-toggle="tab" role="tab">
            <i class="fa fa-user" aria-hidden="true"></i> <span class="d-none d-lg-inline-block">General Info</span></a>
    </li>
    <li class="disabled">
        <a class="nav-link" href="#myTickets" data-toggle="tab" role="tab">
            <i class="fa fa-calendar" aria-hidden="true"></i> <span
                    class="d-none d-lg-inline-block">My Tickets</span></a>
    </li>
    <!-- more list items -->
</ul>

jQuery的:

   var path = window.location.href;

        $('.registration_form_list a').each(function () {
            var hash = $(this).attr('href').split('#')[1];
            //alert(hash); appaers
            if (this.href === path) {
                // alert('test'); dont appears
                $('.registration_form_list a').removeClass('active');
                $('a[href="#'+hash+'"]').addClass('active');
            }
        });

例如,我有一个方法,用户被重定向到“ http://proj.test/user/profile?user = 1 #myTickets”,但当用户访问页面时,保持活动的选项卡是“#generalInfo” 。

return redirect(route('user.index', ['user' => Auth::id()]).'#myTickets');

标签内容位于标签内容中,其对应的ID如下:

<div class="tab-content registration_body bg-white" id="myTabContent">

    <div class="tab-pane fade show active clearfix" id="generalInfo" role="tabpanel" aria-labelledby="home-tab">
    <!-- #generalInfo content -->
    </div>

    <div class="tab-pane clearfix fade" id="myTickets" role="tabpanel" aria-labelledby="contact-tab">
        <!-- #myTickets content -->
    </div>
    <!-- other tabs -->
</div>

试试这个

$(document).ready(function() {
  // Obtain hash value from the url
  var hash = window.location.hash;
  // Try to find a nav-link with the hash
  var hashNavLink = $('.nav-link[href="'+hash+'"]');

  // If there is no link with the hash, take default link
  if (hashNavLink.length === 0) {
    hashNavLink = $('.nav-link[href="#generalInfo"]');
  }

  hashNavLink.addClass('active');
});

在这种情况下,您可以确定如果URL中存在错误的哈希,则#generalInfo链接将被设置为活动,而在其他情况下,正确的链接将始终处于活动状态。

您可以直接从Location获取hash值。

var path = window.location.href; 返回您网址的完整路径。

this.href等于#generalInfo所以它永远不会等于http://proj.test/user/profile?user=1#generalInfo

如果您希望在URL与锚标记的href值之间存在条件,则需要减少path

//This will always assume you're only using 1 pound sign    
var path = window.location.href.split("#")[1];

这样, path = generalInfo (假设你在http://proj.test/user/profile?user = 1#generalInfo

那么你需要创建条件if(hash === path){} (不是if (this.href === path)因为this.href等于#generalInfo而不仅仅是generalInfo

所以现在它将寻找generalInfo === generalInfo

整个代码是:

 var path = window.location.href.split('#')[1]; //equals the hash

    $('.registration_form_list a').each(function () {
        var hash = $(this).attr('href').split('#')[1];
        //alert(hash); appaers
        if (hash === path) {
            // alert('test'); dont appears
            $('.registration_form_list a').removeClass('active');
            $('a[href="#'+hash+'"]').addClass('active');
        }
    });

(作为旁注,我喜欢做的是在这种情况下从所有锚链接中删除active类,然后再向特定的链接添加新链接)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM