JSON序列化一个DOM元素 [英] JSON serialize a DOM element

查看:114
本文介绍了JSON序列化一个DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做:

var el =
{
   o : document.createElement("iframe")
}

var fs = JSON.stringify(el);

and then I try to access with

var ofs = JSON.parse(fs);

ofs.o 包含一个空对象而不是iframe元素为什么??

ofs.o contains an empty object not the iframe elements WHY??

推荐答案

JSON( JavaScript对象表示法)是不是设计用于序列化DOM节点,您需要自己提取所需的内容并将其写入对象,然后根据需要重新创建DOM节点。

JSON (JavaScript Object Notation) is not designed for serializing DOM Nodes, you'll need to pull out the stuff you want by yourself and write it to an object, and then re-create the DOM Nodes from that if you need.

事实上,Chrome甚至不执行您的代码:

In fact, Chrome doesn't even execute your code:

TypeError: Converting circular structure to JSON

这篇关于JSON序列化一个DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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