我有一些列表项在“#registration_form_list”列表中。默认的活动列表项是具有ID“#generalInfo”的列表项。但当访问像这样的网址时,"http://proj.test/user/profile?user=1#myTickets",我想让保持活跃的选项卡是“My Tickets”选项卡。
因此,我想根据当前URL激活选项卡。但下面的jQuery不起作用。
您知道为什么吗?
Html:
例如,我有一个方法,其中用户被重定向到“http://proj.test/user/profile?user=1#myTickets”,但当用户访问页面时,保持活动状态的选项卡是“#generalInfo”。
选项卡内容在具有相应ID的tab-content中,如下所示:
因此,我想根据当前URL激活选项卡。但下面的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的tab-content中,如下所示:
<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>
.ready()
处理程序中? - mpallanschthis.href
中的是什么值? - paudelthis.href === path
更换为hash === window.location.hash.replace('#', '')
,第二个提示会触发吗? - mpallansch