如何使用localStorage保存手风琴状态 [英] How to save the state of accordion using localStorage

查看:119
本文介绍了如何使用localStorage保存手风琴状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存手风琴的状态并在页面刷新后保留它.

I wanted to save the state of the accordion and keeping it after the page refresh.

到目前为止,这里是脚本.

So far here is script.

 <script>
    $(document).ready(function () {
    $('table').accordion({
     header: '.category',
     collapsible: true,
     active: localStorage.getItem('accordion-active')||false
     });
});
  window.onload = function(){
  localStorage.setItem('accordion-active',$('table').accordion('option','active'));
};
 </script>

JFIDDLE

但是似乎我无法使其工作.有什么建议吗?

but it seems I couldn't make it work. Any suggestions?

推荐答案

您可以通过将当前活动手风琴的索引存储在acivate事件中,然后在pageload的active选项中使用它来做到这一点

You would do that by storing the index of the current active accordion in the acivate event, and then use it in the active option on pageload

$(document).ready(function () {
     $('table').accordion({
         header: '.category',
         collapsible: true,
         activate: function(e, ui) {
             localStorage.setItem('accordion-active', $(this).accordion( "option", "active" ));
         },
         active: +localStorage.getItem('accordion-active')
     });
});

FIDDLE

这篇关于如何使用localStorage保存手风琴状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆