How to convert selected HTML to Json?

前端 未结 6 862
迷失自我
迷失自我 2020-12-03 14:27

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?

6条回答
  •  隐瞒了意图╮
    2020-12-03 15:13

    What you want to do is called serializing.

    //  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);
    

提交回复
热议问题