这是我在这里的第一个问题,但我已经潜伏了很长时间。我希望我能说清楚。
碰巧我正在开发一个CI应用程序,带有Bootstrap 3+选项卡,在我进行"表单提交"后,重新加载会将我带到第一个选项卡,而不是最后一个活动标签。
<ul class="nav nav-tabs" id="tabs">
<li class="active"><a href="#accion" data-toggle="tab">Acción</a></li>
<li><a href="#observaciones" data-toggle="tab">Observaciones</a></li>
<li><a href="#adjuntos" data-toggle="tab">Adjuntos</a></li>
</ul>
这就是我的选项卡的构建方式,控制器有一个重定向(),如下所示:
redirect(base_url().'/responderconsulta/index/'.$consulta_id.'#observaciones');
我还没有找到纠正这个问题的方法。我希望我能得到一些帮助。
提前谢谢。
感谢@Paul Zepernick,他在评论中提到了这篇文章,这段代码让这件事变得轻松。
$(document).ready(function() {
if(location.hash) {
$('a[href=' + location.hash + ']').tab('show');
}
$(document.body).on("click", "a[data-toggle]", 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');
});
感谢大家的帮助,我真的很感激!
在您的js文件中:
var hash = window.location.hash;
$('#tabs > li').removeClass("active");
$('#tabs').find('a[href='+hash+']').parent().addClass("active");
目标是检测url末尾的锚点,并将好标签设置为活动
编辑:这里有另一个选择:
var hash = window.location.hash;
$('#tabs').find('a[href='+hash+']').click();