2017-07-06 62 views
0

這是我的選項卡。我想要在頁面重新加載時打開第二個選項卡。我怎樣才能做到這一點如何創建標籤網址

HTML

<li role="presentation" class="active"> 
<a href="#details" aria-controls="details" role="tab" data-toggle="tab">Details</a> 
</li> 
<li role="presentation"> 
<a href="#comments" aria-controls="comments" role="tab" data-toggle="tab">Comments: (<?php echo $comments_count[0]->total?>)</a> 
</li> 

我發現這個jQuery代碼,但它不工作:

// Javascript to enable link to tab 
var url = document.location.toString(); 
if (url.match('#')) { 
    $('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show'); 
} 

// Change hash for page-reload 
$('.nav-tabs a').on('shown.bs.tab', function (e) { 
    window.location.hash = e.target.hash; 
}) 

回答

0

您可以使用這段代碼

Html文件

<ul class="nav nav-tabs" id="myTab"> 
    <li class="active"><a href="#home">Home</a></li> 
    <li><a href="#profile">Profile</a></li> 
    <li><a href="#messages">Messages</a></li> 
    <li><a href="#settings">Settings</a></li> 
</ul> 

<div class="tab-content"> 
    <div class="tab-pane active" id="home">home</div> 
    <div class="tab-pane" id="profile">profile</div> 
    <div class="tab-pane" id="messages">messages</div> 
    <div class="tab-pane" id="settings">settings</div> 
</div> 

JavaScript部分

$('#myTab a').click(function(e) { 
    e.preventDefault(); 
    $(this).tab('show'); 
}); 

// store the currently selected tab in the hash value 
$("ul.nav-tabs > li > a").on("shown.bs.tab", function(e) { 
    var id = $(e.target).attr("href").substr(1); 
    window.location.hash = id; 
}); 

// on load of the page: switch to the currently selected tab 
var hash = window.location.hash; 
$('#myTab a[href="' + hash + '"]').tab('show'); 
+0

我用這個網址進行重新加載。它轉到消息選項卡,但頁面不重新加載。 'success:function(data){ \t window.location.href =「http:// localhost/demo/item-details /?id = 19#messages」;' }, – james