开发者

Serializing div

Trying to make kinda WISYWIG editor th开发者_JAVA技巧at creates a div with some inner elements. The div (with all the inner elements structure), once designed, should be serialized (somehow, hope it's the right term, into string and/or JSON), stored in DB and later inserted to DOM of some other HTML document.

Hence, the questions:

  1. Which is the best way to serialize a div?
  2. Which is the best way to de-serialize a div (and insert it to the DOM)?


You don't serialize the DIV, you serialize the form fields.

You simply grab the DIV and its contents and store these as HTML directly in the DB. It's basically the same as having HTML in a textarea.

You then just inser the HTML back into the DOM.


If you want to grab the HTML code of your DOM elements, you can use innerHTML. See this fiddle for an example:

HTML:

<div id='div1'>
  <p>A Paragraph!</p>
  <br/>
  <p>Another p</p>
</div>
<button id='go'>Get HTML</button>

JS

document.getElementById('go').onclick = function(){ 
  alert(document.getElementById('div1').innerHTML);
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜