我目前在 Twitter Bootstrap 中使用选项卡,并希望在用户发布数据和页面重新加载后选择相同的选项卡。
这是怎么做到的?
我当前对选项卡的调用如下所示:
<script type="text/javascript">
$(document).ready(function() {
$('#profileTabs a:first').tab('show');
});
</script>
我的标签:
<ul id="profileTabs" class="nav nav-tabs">
<li class="active"><a href="#profile" data-toggle="tab">Profile</a></li>
<li><a href="#about" data-toggle="tab">About Me</a></li>
<li><a href="#match" data-toggle="tab">My Match</a></li>
</ul>
最佳答案
您必须使用 localStorage 或 cookie 来管理它。这是一个快速而肮脏的解决方案,可以大大改进,但可能会给您一个起点:
$(function() {
// for bootstrap 3 use 'shown.bs.tab', for bootstrap 2 use 'shown' in the next line
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
// save the latest tab; use cookies if you like 'em better:
localStorage.setItem('lastTab', $(this).attr('href'));
});
// go to the latest tab, if it exists:
var lastTab = localStorage.getItem('lastTab');
if (lastTab) {
$('[href="' + lastTab + '"]').tab('show');
}
});
关于javascript - 重新加载页面后,如何使用 twitter bootstrap 使当前选项卡保持事件状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10523433/