yan*_*yan 12 html javascript jquery 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>
Run Code Online (Sandbox Code Playgroud)
我是json的新手,请尽可能简化.我看过其他问题,但他们似乎没有解决我的问题
lle*_*aff 15
你想要做的是称为序列化.
// 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);
Run Code Online (Sandbox Code Playgroud)
function htmlToJson(div,obj){
if(!obj){obj=[]}
var tag = {}
tag['tagName']=div.tagName
tag['children'] = []
for(var i = 0; i< div.children.length;i++){
tag['children'].push(htmlToJson(div.children[i]))
}
for(var i = 0; i< div.attributes.length;i++){
var attr= div.attributes[i]
tag['@'+attr.name] = attr.value
}
return tag
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49723 次 |
| 最近记录: |