如何将选定的HTML转换为Json? [英] How to convert selected HTML to Json?
本文介绍了如何将选定的HTML转换为Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将部分html代码保存为json文件,然后重述html代码进行编辑.知道我该怎么办吗?
I want to save part of my html code into json as a file then recap back the html codes for editing. Any idea how can i do it?
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1">
<label class="draggable ui-widget-content clickableLabel" id="label1" >New Text</label>
<input id='textbox1' class="clickedit" type="text" class="draggable" class="ui-widget-content" placeholder="Text Here"/>
<div class="clearfix"></div>
</div>
</div>
我是json的新手,请尽可能简化.我曾经看过其他问题,但他们似乎并没有解决我的问题
I am new to json, please simplified if possible.I had look at other questions but their don't seem to address my question
推荐答案
您要执行的操作称为序列化.
// This gives you an HTMLElement object
var element = document.getElementById('TextBoxesGroup');
// This gives you a string representing that element and its content
var html = element.outerHTML;
// This gives you a JSON object that you can send with jQuery.ajax's `data`
// option, you can rename the property to whatever you want.
var data = { html: html };
// This gives you a string in JSON syntax of the object above that you can
// send with XMLHttpRequest.
var json = JSON.stringify(data);
这篇关于如何将选定的HTML转换为Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文