使用本地存储保存div的内容,并在另一页上显示 [英] Save content of div with local storage, and display on another page

查看:139
本文介绍了使用本地存储保存div的内容,并在另一页上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在页面加载时在页面上获取<div>的内容,并将其保存到localstorage.

I am trying to grab the contents of a <div> on a page on page load, and save it to localstorage.

当有人再次访问主页时,我希望显示此div的内容.

When someone visits the homepage again, I would like the contents of this div to be displayed.

虽然我可以使用表单输入字段和提交功能来使功能正常工作,但我找不到仅使用内容和页面加载来实现此功能的方法.

Whilst I can get functions to work with the use of form input fields and a submit, I cannot find a way to get this working using just contents and page load.

任何帮助表示赞赏!

非常感谢

推荐答案

在没有看到您的标记和代码的情况下,很难知道可能是什么问题,您应该能够执行以下操作

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

在第一页上:

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

要在页面上显示内容:

$(function() {
   if (localStorage["myKey"] != null) {
      var contentsOfOldDiv = JSON.parse(localStorage["myKey"]);    
      $("divWhereIwantStuffDisplayed").html(contentsOfOldDiv);
     } 
});

这篇关于使用本地存储保存div的内容,并在另一页上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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