URL不显示Bootstrap 4选项卡

时间:2018-01-24 18:35:02

标签: jquery html twitter-bootstrap tabs bootstrap-4

我有这个HTML结构:

    <div class="row">
        <div class="col-md-3">
            <div class="card p-2">
                <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
                    <a class="nav-link active" id="general-tab" data-toggle="pill" href="#general" role="tab" aria-controls="general" aria-selected="true">Info generali</a>
                    <a class="nav-link" id="permalink-tab" data-toggle="pill" href="#permalink" role="tab" aria-controls="permalink" aria-selected="false">Permalink</a>
                </div>
            </div>
        </div>

        <div class="col-md-9">
            <div class="card p-3">
            <div class="tab-content" id="v-pills-tabContent">

                <!-- Infos tab -->
                <div class="tab-pane fade show active" id="general" role="tabpanel" aria-labelledby="v-pills-home-tab">
                </div>

                <!-- Permalink tab -->
                <div class="tab-pane fade" id="permalink" role="tabpanel" aria-labelledby="permalink-tab">
                </div>
            </div>
            </div>
        </div>
   </div>

当你加载页面时,id =“general”选项卡是第一个出现的,但是我有时需要直接加载第二个标签id =“permalink”但是如果我尝试用URL调用它像这样:

www.somesite.com/edit/4#permalink

不起作用:当页面加载时,它不会调用#permalink选项卡而是#general选项卡。

1 个答案:

答案 0 :(得分:2)

您的标签由JS激活,因此通过片段标识符链接到它们不会做任何事情,因为js隐藏了它们。 您必须使用js从地址栏中的URL获取片段并手动激活选项卡。

示例:

url = somethin#profile

如果链接中有#,则显示标签

你可以将它设置为一个功能,并在加载dom后立即加载它,或者你可以听一个事件。

欢呼声

&#13;
&#13;
//var url = document.location.toString();
console.log('example link');
var url = 'something#profile';
if (url.match('#')) {
    $('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
} 
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha/js/bootstrap.min.js"></script>
<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" data-toggle="tab" href="#home" role="tab">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-toggle="tab" href="#profile" role="tab">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-toggle="tab" href="#messages" role="tab">Messages</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" data-toggle="tab" href="#settings" role="tab">Settings</a>
  </li>
</ul>

<div class="tab-content">
  <div class="tab-pane active" id="home" role="tabpanel">HOME</div>
  <div class="tab-pane" id="profile" role="tabpanel">PROFILE</div>
  <div class="tab-pane" id="messages" role="tabpanel">MESSAGE</div>
  <div class="tab-pane" id="settings" role="tabpanel">SETTINGS</div>
</div>


<div class="row">
  <div class="col-md-12">
    <div id="tc"></div>
  </div>
</div>
&#13;
&#13;
&#13;