2017-07-30 76 views
0

我正在開發一個具有Bootstrap動態選項卡的網頁。我在鏈接外部頁面的標籤內容時遇到了問題。
我想從另一頁的外部鏈接激活下面頁面的菜單1和菜單2選項卡。
嘗試使用href =「index.html#menu1」但它不起作用。如何從bootstrap的另一頁激活動態選項卡?

<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <title>index</title> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 

 
<div class="container"> 
 
    <div class="row"> 
 
    <div class="col-sm-3"> 
 
    <ul class="nav nav-pills nav-stacked"> 
 
    <li class="active"><a data-toggle="pill" href="#menu1">Menu 1</a></li> 
 
    <li><a data-toggle="pill" href="#menu2">Menu 2</a></li> 
 
    </ul> 
 
    </div> 
 
    <div class="col-sm-9"> 
 
    <div class="tab-content"> 
 
    <div id="menu1" class="tab-pane fade in active"> 
 
     <h3>Menu 1</h3> 
 
     <p>Menu 1 activated.</p> 
 
    </div> 
 
    <div id="menu2" class="tab-pane fade"> 
 
     <h3>Menu 2</h3> 
 
     <p>Menu 2 Activated </p> 
 
    </div> 
 
    </div> 
 
</div> 
 
</div> 
 
</body> 
 
</html>

回答

0

感謝this answer about query stringthis answer about to show Bootstrap's tab,添加下面的腳本到你的頁面,並生成通過查詢字符串在其他網頁上的鏈接!

function getQuery(url) { 
 
    var query = {}, 
 
     href = url || window.location.href; 
 
    href.replace(/[?&](.+?)=([^&#]*)/g, function (_, key, value) { 
 
     query[key] = decodeURI(value).replace(/\+/g, ' '); 
 
    }); 
 
    return query; 
 
} 
 

 
function getParam(name){ 
 
    var obj = getQuery(); 
 
    return obj[name]; 
 
} 
 

 
$(document).ready(function() { 
 
    var target = getParam('target'); 
 
    $('.nav a[href="#'+target+'"]').tab('show'); 
 
});
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <title>index</title> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
</head> 
 
<body> 
 

 
<div class="container"> 
 
    <div class="row"> 
 
    <div class="col-sm-3"> 
 
    <ul class="nav nav-pills nav-stacked"> 
 
    <li class="active"><a data-toggle="pill" href="#menu1">Menu 1</a></li> 
 
    <li><a data-toggle="pill" href="#menu2">Menu 2</a></li> 
 
    </ul> 
 
    </div> 
 
    <div class="col-sm-9"> 
 
    <div class="tab-content"> 
 
    <div id="menu1" class="tab-pane fade in active"> 
 
     <h3>Menu 1</h3> 
 
     <p>Menu 1 activated.</p> 
 
    </div> 
 
    <div id="menu2" class="tab-pane fade"> 
 
     <h3>Menu 2</h3> 
 
     <p>Menu 2 Activated </p> 
 
    </div> 
 
    </div> 
 
</div> 
 
<div class="row"> 
 
use the following script and 
 
the link on the other pages should be like this: url?target=menu2 
 
without "#" 
 
</div> 
 
</div> 
 
</body> 
 
</html>

相關問題