在页面的 js 代码里引入下面这段代码
/*Bootstrap 3:在页面刷新时保持选中的选项卡*/
$(document).ready(function () {
if (location.hash) {
$("a[href='" + location.hash + "']").tab("show");
}
$(document.body).on("click", "a[data-toggle='tab']", function (event) {
location.hash = this.getAttribute("href");
});
});
$(window).on("popstate", function () {
var anchor = location.hash || $("a[data-toggle='tab']").first().attr("href");
$("a[href='" + anchor + "']").tab("show");
});
示例代码(可以直接运行)
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例 - 标签页(Tab)插件事件</title>
<link href="http://libs.baidu.com/bootstrap/3.0.3/