如何在引导程序中从其他页面激活动态选项卡?

时间:2017-07-30 14:38:04

标签: javascript twitter-bootstrap

我正在开发一个包含Bootstrap动态标签的网页。我在链接外部页面的标签内容时遇到问题。
我想从另一页的外部链接激活下面页面的菜单1和菜单2选项卡。
尝试使用href =“index.html#menu1”但它不起作用。< / p>

<!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>

1 个答案:

答案 0 :(得分:1)

感谢this answer about query stringthis answer about to show Bootstrap's tab,将以下脚本添加到您的网页,并使用查询字符串在其他网页上生成链接!

&#13;
&#13;
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');
});
&#13;
<!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>
&#13;
&#13;
&#13;