Use HTML5 localstorage to retain jQuery toggle state on page refresh

前端 未结 1 1645
逝去的感伤
逝去的感伤 2020-12-17 03:39

I just learned about the HTML5 \"localstorage\" function. I have been attempting to implement it in conjunction with jQuery toggle in order to cause a div to remain in it\'s

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 04:06

    Try

    $(document).ready(function () {
        $('#foo').click(function () {
            $(this).siblings().toggle();
            //you need to pass string values, your variables display & block was not defined
            localStorage.setItem('display', $(this).siblings().is(':visible'));
        });
        var block = localStorage.getItem('display');
        if (block == 'true') {
            $('#bar').show()
        }
    });
    

    Demo: Fiddle

    0 讨论(0)
提交回复
热议问题