即使关闭浏览器后,也会将附加元素存储到div

时间:2017-06-13 09:46:48

标签: javascript jquery html

附加到div的列表,如何存储它们以便下次打开浏览器时附加的元素保持为先前的状态。 “$('#try')。prepend(''+ $('#dat')。html()+'');”这就是我要追加的内容

 //This is the empty div

    <div id="dat" class="alert alert-danger">

    </div>

    /*below is my javascript code to append elements(ignore other functions)
    this code basically appends the defects to the empty div*/

    var queue = new Array();

    function greet(greeting) {
        console.log(greeting);
        $('#dat').text(greeting);
    }

    function getRandom(arr) {
        var rand = Math.random();
        return arr[Math.floor(rand * arr.length)];
    }

    var greetings = [
        "Hello 1",
        "hello 2",
        "hello 3",
        "Hello 4",
        "hello 5",
        "hello 6",
        "hello 7"
    ];

    /*---------------------------*/


    $(document).ready(function() {
        ion.sound({
            sounds: [

                { name: "glass" }
            ],

            path: "Scripts/ui control/ring/",
            preload: false,
            volume: 10.0
        });
        setInterval(function() {
            ion.sound.play("glass");
            greet(getRandom(greetings));

            $(".alert").fadeIn("fast").fadeOut(5000, function addMessage(msg) {
                queue.push(msg);
                $('#try').prepend('<li class="list-group-item danger">' + $('#dat').html() + '</li>');

                if ($('.list-group-item').length > 9) {
                    //var msgToRemove = queue.shift();
                    //msgToRemove.remove();
                    $('.list-group-item:last').remove();
                }
            });
        }, 8000);

    });

1 个答案:

答案 0 :(得分:0)

您可以在localStorage变量中存储您想要的任何内容,然后再使用它:

localStorage.stored = ... ; //Whatever you want to store

...

if(typeof localStorage.stored != 'undefined'){
    $('mySelector').append(localStorage.stored);
}