如何将javascript html dom文档存储到locastorage [英] how to store javascript html dom document to locastorage

查看:85
本文介绍了如何将javascript html dom文档存储到locastorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件(组件树)'m',我试图存储在html5的本地存储中。我尝试将其设置为本地存储。但当我将其取回时,此m已更改为[object Document]。
如何将文档存储到本地存储并将其作为文档本身检索?

I have document (component tree) 'm' which i am trying to store in the local storage of html5. I tried setting it to local storage. but when i retrieve it back, this m has changed to [object Document]. How do i store document to local storage and retrieve it as document itself?

以下是我尝试过的代码

应用程序向服务器发送ajax请求以退出日历的事件,并在请求的onsuccess中收到xhtml的dom表示。
当用户进入下周视图时,应用程序必须从本地存储中撤消该事件。要从js文件向客户端发送响应,dom表示也是必要的。因此我们需要将dom表示存储到localstorage。

the application sends a ajax request to server to retireve events of a calendar and in the request's onsuccess the dom representation of xhtml is recieved. when the user goes to next week view, the app has to retieve the event from the localstorage. to send the response to the client from the js file, the dom representation is also necessary.so we need to store the dom representation to the localstorage.

我的要求是第一张图片的控制台,你可以看到'k'从文件m中检索出来并作为响应发送到客户端。所以,我想要的是将这个k或m存储到本地存储,这样我就可以在同一个js中操作它file.this代码在一个单独的js文件中,用于xhtml文件。

my requirements is in 1st picture's console, you can see 'k' is retrieved from the document m and is send as response to the client side.So, what I want is to store this k or m to local storage so i can manipulate it in the same js file.this code is in a separate js file which is used in the xhtml file.

我使用的是primefaces 4.0,jsf 2.1。

I am using primefaces 4.0, jsf 2.1.

当我使用

localStorage.setItem(calendarevents,JSON.stringify(k));

localStorage.setItem("calendarevents",JSON.stringify(k));

我收到错误'将循环结构转换为json'。

I get an error 'converting circular structure to json'.

推荐答案

你需要存储一个字符串表示执行文档的HTML

You need to store a string representation of the document's html by doing:

localStorage.setItem('calendar', document.documentElement.innerHTML);

执行此操作时:

localStorage.setItem('calendar', document.documentElement);

...它存储 document.documentElement.toString()的结果在localStorage中,这不适用于您的目的。

...it stores the result of document.documentElement.toString() in localStorage, which doesn't work for your purpose.

这篇关于如何将javascript html dom文档存储到locastorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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