show display:none div after refresh

前端 未结 5 921
滥情空心
滥情空心 2020-12-17 00:34

Don\'t know how to put the question title correctly! I have a div that is displayed by button click. The problem is that if user goes to next page(by clicking another button

5条回答
  •  离开以前
    2020-12-17 00:40

    You can use the html5 webStorage for this:

    localStorage does not expire, whereas sessionStorage gets deleted when the browser is closed (usage of both is equivalent). The webStorage is support by all major browsers and IE >= 8

    Plain Javascript

    function showTable() {
       document.getElementById('tableDiv').style.display = "block";
       localStorage.setItem('show', 'true'); //store state in localStorage
    }
    

    And check the state onLoad:

    window.onload = function() {
        var show = localStorage.getItem('show');
        if(show === 'true'){
             document.getElementById('tableDiv').style.display = "block";
        }
    }
    

    jQuery

    function showTable() {
        $('#tableDiv').show();
        localStorage.setItem('show', 'true'); //store state in localStorage
    }
    
    $(document).ready(function(){
        var show = localStorage.getItem('show');
        if(show === 'true'){
            $('#tableDiv').show();
        }
    });
    

    Demo

    P.S. To remove an item from the localStorage use

    localStorage.removeItem('show');
    

    Reference

    webStorage

提交回复
热议问题