Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将本地存储中的任何内容加载到其他页面_Javascript_Html_Json_Local Storage - Fatal编程技术网

Javascript 如何将本地存储中的任何内容加载到其他页面

Javascript 如何将本地存储中的任何内容加载到其他页面,javascript,html,json,local-storage,Javascript,Html,Json,Local Storage,我正在使用contenteditable,我想使用本地存储将我在其中写入或上载的所有内容移动到另一个页面 假设这是第一个带有contenteditable的页面,当点击发布按钮时,它会将所有内容保存到本地存储并加载到第二个页面,即view.html post.html <div id="editor1" contenteditable="true" class="editable" ></div> <b

我正在使用
contenteditable
,我想使用本地存储将我在其中写入或上载的所有内容移动到另一个页面

假设这是第一个带有
contenteditable
的页面,当点击发布按钮时,它会将所有内容保存到本地存储并加载到第二个页面,即view.html

post.html

<div id="editor1" contenteditable="true"  class="editable" ></div>

<button id="publishDoc">Publish</button>

<div class="post-body" id="postbody">

</div>

我有这个代码,它保存到本地存储,但加载到同一页。但是我想把它放到另一个页面

JS

const editables = document.querySelectorAll("[contenteditable]");

// save edits
editables.forEach(el => {
  el.addEventListener("blur", () => {
    localStorage.setItem("dataStorage-" + el.id, el.innerHTML);
  })
});

// once on load
for (var key in localStorage) {
  if (key.includes("dataStorage-")) {
    const id = key.replace("dataStorage-","");
    document.querySelector("#" + id).innerHTML = localStorage.getItem(key);
  }
}


希望有人能帮我。谢谢。

到底什么不起作用?只需在另一页上加载一次后复制和/或执行代码即可。当然,由于您的逻辑,您必须使用相同的
id
,但是如果它在post.html上工作,那么它也应该在view.html上工作。旁注:将选择器更改为
document.queryselectoral(“[contenteditable='true']”)
@Lain我现在尝试了,但效果不错,我一直用错误的方法。非常感谢你,先生。