Save content of div with local storage, and display on another page

后端 未结 1 1666
别那么骄傲
别那么骄傲 2021-01-17 03:48

I am trying to grab the contents of a

on a page on page load, and save it to localstorage.

When someone visits the homepage again, I would

相关标签:
1条回答
  • 2021-01-17 04:32

    Without seeing your markup and code it's hard to know what might be the problem, you should be able to do something like the following

    On the first page:

    $(function() {
      localStorage["myKey"] = JSON.stringify($("#divWithContents").html());
    });
    

    On the page you want the contents displayed:

    $(function() {
       if (localStorage["myKey"] != null) {
          var contentsOfOldDiv = JSON.parse(localStorage["myKey"]);    
          $("divWhereIwantStuffDisplayed").html(contentsOfOldDiv);
         } 
    });
    
    0 讨论(0)
提交回复
热议问题