2017-07-06 1 views
0

このマイタブ。ページリロードが2番目のタブを開いたときにしたい。どのように私はタブの作成方法

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

私はリロードのためにこのURLを使用しています。メッセージタブに移動しますが、ページはリロードされません。 '成功:function(data){ \t window.location.href =" http:// localhost/demo/item-details /?id = 19#messages "; ' } – james

関連する問題