JSON serialize a DOM element

前端 未结 4 1510
孤街浪徒
孤街浪徒 2020-12-18 13:09

If I do:

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

var fs = JSON.stringify(el);

and then I try to access with

var ofs = JSON.parse(fs);
         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-18 14:07

    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.

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

    TypeError: Converting circular structure to JSON
    

提交回复
热议问题