将部分html保存到本地存储以后加载

我需要将html div(id = tabela)的内容保存到localstorage,以便可以通过localstorage在另一个会话中加载表的内容。 如何将div内容变为变量?

var = tabelaContent; //<-- I need contents of 
in this localStorage.setItem("tabelaContent",tabelaContent); // then, upon pressing the "load" button localStorage.getItem(tabelaContent);

仅供参考,如果还有另一种方法可以使表的内容持久,请告诉我

TIA。

您可以使用document.getElementById("myID").innerHTML ,如下所示:

 var = tabelaContent; tabelaContent = document.getElementById("tabela").innerHTML localStorage.setItem("tabelaContent",tabelaContent); 

这是你在寻找的。

 
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

查看此 工作演示