gpt4 book ai didi

javascript - 如何从 Bootstrap 中的另一个页面激事件态选项卡?

转载 作者:行者123 更新时间:2023-12-01 03:18:10 31 4
gpt4 key购买 nike

我正在开发一个网页,其中包含来自 Bootstrap 的动态选项卡。我在从外部页面链接选项卡内容时遇到问题。
我想从另一个页面的外部链接激活下面页面的菜单 1 和菜单 2 选项卡。
尝试使用 href="index.html#menu1"但不起作用。

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

最佳答案

感谢 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>

关于javascript - 如何从 Bootstrap 中的另一个页面激事件态选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45400961/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com