小编典典

如何将选定的HTML转换为Json?

json

我想将部分html代码保存为json文件,然后重述html代码进行编辑。知道我该怎么做吗?

<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的新手,请尽可能简化我曾经看过的其他问题,但它们似乎并没有解决我的问题


阅读 430

收藏
2020-07-27

共1个答案

小编典典

您要执行的操作称为 序列化

//  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);
2020-07-27