在页面刷新/导航上保留Twitter Bootstrap Collapse状态

我正在使用Bootstrap“collapse”插件为一长串链接制作手风琴。 accordion-body标签包含“折叠”,因此页面加载时所有组都会折叠。 当您打开一个组并单击一个链接时,它会将您带到一个新页面以查看一些详细信息,然后单击后一个链接或浏览器返回该列表。 不幸的是,当你返回时,手风琴回到了折叠状态,你必须再次打开这个组,找到你的位置。 我期待很多这种来回导航,这种行为将令人沮丧。

有没有办法保留用户的位置并返回到它,或者只是阻止页面重新加载或再次触发javascript。

我认为解决方案可能是沿着这些方向,但不确定。 Twitter引导程序:在开放式手风琴标题中添加一个类

您可以通过cookie轻松解决此问题。 有很多简化的库,比如我在下面的例子中使用的https://github.com/carhartl/jquery-cookie :

 

将以下代码添加到脚本部分( #accordion2指的是修改后的twitter bootstrap示例,我之后列出)

 $(document).ready(function() { var last=$.cookie('activeAccordionGroup'); if (last!=null) { //remove default collapse settings $("#accordion2 .collapse").removeClass('in'); //show the last visible group $("#"+last).collapse("show"); } }); //when a group is shown, save it as the active accordion group $("#accordion2").bind('shown', function() { var active=$("#accordion2 .in").attr('id'); $.cookie('activeAccordionGroup', active) }); 

你完成了! 这里是http://twitter.github.com/bootstrap/javascript.html#collapse中带有可点击链接的示例的修改版本,当您返回时 – 最后显示的手风琴组会自动打开

  

Bootstrap 3.xx中,您必须使用以下脚本来保存cookie中的最后一个打开状态。

HTML标记

 
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.

jQuery的

  $(document).ready(function () { //when a group is shown, save it as the active accordion group $("#accordion").on('shown.bs.collapse', function () { var active = $("#accordion .in").attr('id'); $.cookie('activeAccordionGroup', active); // alert(active); }); $("#accordion").on('hidden.bs.collapse', function () { $.removeCookie('activeAccordionGroup'); }); var last = $.cookie('activeAccordionGroup'); if (last != null) { //remove default collapse settings $("#accordion .panel-collapse").removeClass('in'); //show the account_last visible group $("#" + last).addClass("in"); } }); 

我尝试了上面建议的技术,它对我有用(有点)但是调用.collapse(“show”)似乎在某些情况下打破了手风琴切换行为。 打开第一个面板会使先前打开的面板处于打开状态。 我通过添加类“in”代替了jQuery:

 $(document).ready(function() { var last=$.cookie('activeAccordionGroup'); if (last!=null) { //remove default collapse settings $("#accordion .panel-collapse").removeClass('in'); //show the account_last visible group $("#" + last).addClass("in"); } }); 

否则,谢谢davidkonrad让我走上正轨。

谢谢你,这很有效。 我修改了一下以简单地保留特定DIV的显示/隐藏状态(并不特定于在DIV列表中仅显示一个DIV)。

    

多亏了这个,发现它非常有用,但是如果你使用的是Bootstrap 3和最新的jquery,这可行:

 $("#accordion").on('shown.bs.collapse', function() { ... }); 

希望这能节省一些时间……

另一个可用选项是使用url哈希。

 $(document).ready(function () { var hash = window.location.hash; if (hash) { $("#accordion .panel-collapse").removeClass('in'); $(hash).addClass('in'); } $('#accordion').on('shown.bs.collapse', function () { var activeId = $("#accordion .in").attr('id'); window.location.hash = activeId; }); $('#accordion').on('hidden.bs.collapse', function () { window.location.hash = ''; }); });